Xakir K Moideen
Xakir K Moideen

Reputation: 157

How to add dot in htaccess rewrite rule regex?

I want to create my url structure like dir view (example.com/dir1/dir2/file) For which I am using htaccess mod rewrite as

RewriteEngine on
RewriteBase /
RewriteRule ^/?([a-zA-Z0-9\-=&_@/]+)/?$ get.php?u=$1 [QSA,L]

The code above works fine except that if I try to add dot like

RewriteEngine on
RewriteBase /
RewriteRule ^/?([a-zA-Z0-9\-=&_@/.]+)/?$ get.php?u=$1 [QSA,L]

The .htaccess breaks. "Internal Server Error"

Upvotes: 3

Views: 1133

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

The problem is that if you add a dot in the pattern then your pattern also matches the target url get.php and rewrites get.php to get.php on second rewrite cycle. This results in an infinite loop/Rewrite loop error.

You need a RewriteCond to prevent this :

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/get.php
RewriteRule ^/?([a-zA-Z0-9\-=&_@/.]+)/?$ get.php?u=$1 [QSA,L]

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98961

You need escape the dot \. , i.e.:

RewriteEngine on
RewriteBase /
RewriteRule ^/?([a-zA-Z0-9\-=&_@/\.]+)/?$ get.php?u=$1 [QSA,L]

Upvotes: 1

Related Questions