Asked  7 Months ago    Answers:  5   Viewed   30 times

I have this in my .htaccess file:

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]

but whenever I access a file on my root like http://example.com/robots.txt it will redirect to http://www.example.comrobots.txt/.

How can I correct this so that it will redirect correctly to http://www.example.com/robots.txt?

 Answers

50

Change your configuration to this (add a slash):

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 

Or the solution outlined below (proposed by @absiddiqueLive) will work for any domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

If you need to support http and https and preserve the protocol choice try the following:

RewriteRule ^login$ https://www.%{HTTP_HOST}/login [R=301,L]

Where you replace login with checkout.php or whatever URL you need to support HTTPS on.

I'd argue this is a bad idea though. For the reasoning please read this answer.

Tuesday, June 1, 2021
 
Semirix
answered 7 Months ago
93

Basically what people try to say is, you can make a rewrite rule like so:

RewriteRule ^(.*)$ index.php?params=$1 [NC, QSA]

This will make your actual php file like so:

index.php?params=param/value/param/value

And your actual URL would be like so:

http://url.com/params/param/value/param/value

And in your PHP file you could access your params by exploding this like so:

<?php

$params = explode( "/", $_GET['params'] );
for($i = 0; $i < count($params); $i+=2) {

  echo $params[$i] ." has value: ". $params[$i+1] ."<br />";

}

?>
Wednesday, March 31, 2021
 
bumperbox
answered 9 Months ago
69

Can someone chime in and help me out with the "path" this application is taking before running any php files? Is it redirecting everything to cakeappwebroot, then to whatever redirect is setup beyond the .htaccess files from there? (in this case, to cakeuserslogin)

In short, yes. There is only one .htaccess file doing the real work, and that is "C" (which is the file in /app/webroot/.htaccess). This passes any requests that aren't for an existing file or directory to index.php which bootstraps CakePHP and handles the request. Any further "redirection" is handled by CakePHP's routing.

Edit: To address your question about what's redirecting to the login page, chances are you have configured the Auth component and haven't set any "public" pages via $this->Auth->allow().


The other two are there in case you put the CakePHP installation directory or the app directory into a folder that is used by Apache to serve pages, e.g. /var/www/html or similar. You'll note that the structure is:

/cakephp-1.3.x/.htaccess ("A")

/cakephp-1.3.x/app/.htaccess ("B")

/cakephp-1.3.x/app/webroot/.htaccess ("C")

So, any request at any of these levels will end up being handled by the RewriteRule in "C". This is done to protect sensitive data such as your database connection information and ensure the application functions properly (as all requests should go through the CakePHP bootstrapper, unless you've set up custom routing).

Saturday, May 29, 2021
 
eliotlencelot
answered 7 Months ago
20

It should be something like this:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Thursday, July 1, 2021
 
Slinky
answered 5 Months ago
37

Try:


RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.mysite.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]

//OR
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Saturday, July 31, 2021
 
palAlaa
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share