Mobile app version of vmapp.org
Login or Join
Looi9037786

: .htaccess RewriteRule contains wrong value 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

@Looi9037786

Posted in: #Apache #Htaccess

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 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= [NC,L]

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Looi9037786

1 Comments

Sorted by latest first Latest Oldest Best

 

@Sims2060225

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= [NC,L]


or

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


or even

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


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme