Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a RewriteRule that is not behaving as I expect. I want site.com/author/fred to map to site.com/author/index.php?a=fred

I have an author directory within the site root. The author directory contains the .htaccess file below.

My problem is that $1 contains the value index.php instead of the regex match.

The entire .htaccess file is below. Can you see my mistake? Thank you.

Options +FollowSymlinks
RewriteEngine on

RewriteRule ([^/]+)/?$  index.php?a=$1 [NC,L]
share|improve this question

1 Answer 1

up vote 7 down vote accepted

Your pattern ([^/]+)/?$ will match fred as well as index.php.

The key here -- the way how [L] flag works. After initial rewrite of fred occurs it goes to next rewrite iteration, where it will rewrite index.php?a=fred to index.php?a=index.php. Because URL was rewritten, it goes to 3rd iteration, where index.php?a=index.php will be rewritten to the same URL index.php?a=index.php. Because input and output URLs are the same, rewrite exits and you have what you have right now.

You need to add a condition to prevent looping. For example (one of the possible approaches):

RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule ([^/]+)/?$  index.php?a=$1 [NC,L]

or

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+)/?$  index.php?a=$1 [NC,L]

or even

RewriteRule (?!index\.php)([^/]+)/?$  index.php?a=$1 [NC,L]

Read a bit more: RewriteRule Last [L] flag not working?

share|improve this answer
    
Thanks, works perfect. –  Eric Sep 23 '11 at 19:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.