1

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]
8

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?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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