Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess - stop processing if rewriterule true?

Here is my .htaccess file:

Options +FollowSymlinks
RewriteEngine on

Rewritecond %{http_host} ^mysite.com [nc]
Rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . /404.php

The 1st rewritecond / rewriterule block is to redirect the user to www if they entered the address without www.

The 2nd rewritecond / rewriterule block is to redirect the user to a page that I created if they enter a non-existent address.

Each of these rules works on its own:

If my .htaccess file contains only the first block, then it successfully redirects non-www addresses to the www address.

And if a non-existent address is entered, the user is successfully redirected to my page 404.php.

However, if the .htaccess file contains both blocks, and a non-www address is entered, then instead of a successful redirection to the www address, the following happens:

(1) The following is displayed on the page:

Moved Permanently

The document has moved here.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Apache/1.3.41 Server at mysite.com Port 80

(2) The following entry is generated in the error log:

[Fri May 4 01:54:19 2012] [error] [client 109.109.109.109] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.

I assume that what happens is that an infinite loop is somehow being generated.

I'm wondering if telling the .htaccess file to stop processing commands if the first condition has been met would solve the problem. So does anyone know how to do this?

(Side note. In general, I find the documentation out there on .htaccess to be absolutely horrendous. I wonder if anyone else feels similarly.)

Thanks in advance.

like image 553
oyvey Avatar asked May 04 '12 08:05

oyvey


3 Answers

The [L] flag.

http://httpd.apache.org/docs/2.4/rewrite/flags.html

like image 98
mattmanser Avatar answered Nov 09 '22 07:11

mattmanser


Can you try this code instead:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

ErrorDocument 404 404.php

Rewritecond %{HTTP_HOST} ^mysite\.com$ [NC]
Rewriterule ^ http://www.mysite.com%{REQUEST_URI} [R=302,NC,L]

Once you verify that it's working fine then you change R=302 to R=301.

like image 34
anubhava Avatar answered Nov 09 '22 07:11

anubhava


change one line

Rewriterule ^(.*)$ http://www.mysite.com/ [r=301,nc,L,PT]

like image 1
user1401768 Avatar answered Nov 09 '22 08:11

user1401768