Asif Iqbal
Asif Iqbal

Reputation: 1236

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?

Upvotes: 4

Views: 84

Answers (1)

anubhava
anubhava

Reputation: 784908

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.

Upvotes: 3

Related Questions