Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteRule is not working with plus (+ or *) character

RewriteRule ^([a-z]).php$  /index.php?zig=$1 [NC,L] # working

This rule is working correctly. But

RewriteRule ^([a-z]+).php$  /index.php?zig=$1 [NC,L] # not working

or

RewriteRule ^([a-z]\+).php$  /index.php?zig=$1 [NC,L] # not working

Is not working. Difference is (+). How to use + in the code above?

like image 980
Asif Iqbal Avatar asked May 17 '15 15:05

Asif Iqbal


1 Answers

This rule is fine:

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

but will create an infinite loop since rewritten URI /index.php also matches the regex pattern. To prevent this you need couple of changes like preventing files/directories from this rewrite and escape the dot as it is a special regex meta character:

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)\.php$ index.php?zig=$1 [QSA,NC,L]

QSA (Query String Append) flag preserves existing query parameters while adding a new one.

like image 152
anubhava Avatar answered Sep 29 '22 10:09

anubhava