CakePHP pretty URL redirect - php

Due to my hosting company's policies I am using pretty url in CakePHP (without the Apache mod rewrite). My homepage is at:
http://stthomasedu.org/index.php/home
How can I redirect traffic from
'http://stthomasedu.org/' to 'http://stthomasedu.org/index.php/home' ?
I've tried to edit index.php with
header("Location:http://stthomasedu.org/index.php/home");
but it's not working, every time I have to manually enter the URL "http://stthomasedu.org/index.php/home"
Any ideas?
SOLVED : IN C-PANEL I HAVE AN OPTION TO REDIRECT ALL MY TRAFFIC TO (http://stthomasedu.org) ANY LOCATION (now i set it to http://stthomasedu.org/index.php/home)

Since you are using Apache, you can add these lines in a .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^stthomasedu.org$
RewriteRule ^$ http://stthomasedu.org/index.php/home [L,R=301]

Did you remember to exit after your header() call?
header('Location: /index.php/home');
exit;

Related

How to redirect static URL

I am having a php html site hosted on server. I've url like http://www.test.com/sales.php when user click on the link of this url then it should show www.test.com/services/sales.php instead of www.test.com/sales.php.
I've tried following code but it is not working.
RewriteEngine On
RewriteRule ^http://www.test.com/services/sales.php$ http://www.test.com/sales.php [R=301,NE,NC,L]
You can use like that
htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^sales/? http://www.test.com/services/sales.php [R=301,L]
This is the more robust version that can also be used in the real http server configuration:
RewriteEngine On
RewriteRule ^/?sales\.php$ /services/sales.php [R=301]
RewriteRule ^/?services/sales\.php$ /sales.php [END]
If you want to have a clean URL ("https://example.com/services/sales" instead of "https://example.com/services/sales.php"), then this variant would make sense:
RewriteEngine On
RewriteRule ^/?sales\.php?$ /services/sales [R=301]
RewriteRule ^/?sales/?$ /services/sales [R=301]
RewriteRule ^/?services/sales/?$ /sales.php [END]
It is a good idea to start with a 302-redirection and only change that to a 301 once everything is working as expected. When using a 301 right away you might run into issues with caching, so you should always use a fresh anonymous browser tab when testing.

htaccess simulate subdomain to subdirectory in Wordpress

I need to develop a plugin that can write into the WP htaccess file and "simulate" subdomains.
I have a new WP installation with basic htaccess on example.com
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Is there a way via htaccess that if I go to about.example.com I see the page example.com/about, without changing the URL (meaning that I still see about.example.com in the browser address bar)?
And also, is it possible that if I go to about.example.com/category1 I see the page example.com/about/category1?
I tried using the following code by adding it to the end of the htaccess, but it doesn't work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com.com$
RewriteRule ^(/)?$ about [L]
You should be able to do that using the following rule in your .htaccess:
RewirieEngine On
RewriteCond %{HTTP_HOST} ^about.example.com [OR]
RewriteCond %{HTTP_HOST} ^www.about.example.com
RewriteRule ^(.*) http://example.com/about/$1 [P]
What this does is check for the address about.example.com, if the condition is met it should redirect to example.com/about/.... but keeping the original address.
The [P] flag causes the request to be handled by mod_proxy, and handled via a proxy request.
However, keep in mind that using mod_proxy can cause security issues:
Take care when constructing the target URL of the rule, considering
the security impact from allowing the client influence over the set of
URLs to which your server will act as a proxy. Ensure that the scheme
and hostname part of the URL is either fixed, or does not allow the
client undue influence.
Normally using .htaccess you cannot go from one domain to another domain keeping the original URL. It can be a security nightmare as mentioned above. However, since this is a subdomain it should work.
Make sure you clear your cache before testing this.
Alternative
You can use ProxyPass - which would be a more secure option. You can find more information on how to use this via the Apache Documentation via this link. But this would need to done on a level higher than .htaccess. Through the config files.
I hope this helps.

Changing the actual URL without changing display URL using htaccess [duplicate]

So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.

.htaccess URL rewrite works, but address bar then shows 'dirty' URL

User clicks link (from their email): http://www.site.com/edit/wih293f73y
Browser window opens and gets them to the correct page.
But now the browser's address bar shows: http://www.site.com/editor.php?editCode=wih293f73y
Extra info:
My rewrite rule is:RewriteRule ^edit/([A-Za-z0-9-]+)/?$ editor.php?editCode=$1 [NC,L]
This problem ONLY occurs when the user has clicked a link. It works perfectly when you just type the pretty url into the address bar.
This problem ONLY occurs for links that include the www. - the link http://site.com/edit/wih293f73y works like a charm.
My .htaccess file includes the following code (from HTML5 boilerplate, which I wasn't aware of previously):
# Rewrite www.example.com → example.com
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
If it's important, this occurs after my other rewrite rules.
I just took a look and it is apparent that your www rules is causing this. Question is do you want it be fixed? If you do then move this rule on top of all other rules and your problem should be fixed.
Move this to top of all other rules
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
You can use use the redirect directive
redirect 301 ^edit/([A-Za-z0-9-]+)/?$ editor.php?editCode=$1
There are some pros and cons to this strategy. The pros being;
It's super fast. You don't even need to load up your application for this to work.
It's minimal code.
Redirects are an intrinsic part of Apache (or any http server) and aren't going anywhere soon.
The cons being;
It's not part of your application proper. Should you decide to change logic or URLs, you'll have to change this, too.
It's slower, as you need to invoke php, as opposed to just having Apache issue the redirect.

Changing of www makes session expire

I have a merchant website linked to a bank.
I have defined this success URL in bank :
http://www.mydomain.com/deposit.php
When the user starts payment process he enters to bank website and after payment he redirects to the success URL in my website.
If a user enters my website with this URL (without www) :
http://mydomain.com/
The session expires and he should login again. but when he enters with this URL :
http://www.mydomain.com/
The session will not expire and everything is okey. how can i solve this problem?
The thing is your session cookie is probably specifically tied to www.mydomain.com, so, when you access mydomain.com, the browsers chooses not to send the session cookie.
Take a look at ini_set function, and the session.cookie_domain value.
if (count(explode('.', $domain)) > 2) {
ini_set('session.cookie_domain', $domain);
}
You can force www. (or force using just http://mydomain.com aswell) using the .htaccess file.
RewriteCond %{HTTP_HOST} !^www.mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R]
The ! in the RewriteCondition tells the server that when the address does NOT equal the following, redirect it to the RewriteRule.
You can find alot more Rewrite tips 'n tricks using .htaccess on the web.
Note: For this to work, you will need to enable this first in your .htaccess file:
RewriteEngine On
And make sure your Apache (or other host) configuration supports mod_rewrite.
Taken from the HTML5 Boilerplate .htaccess:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Placing that in your .HTACCESS file should fix the problem. However, someone else will have to elaborate on what exactly is the cause of this problem.
Edit: Seems to me like Pablo Santa Cruz is right.

Categories