Home Ask Login Register

Developers Planet

Your answer is one click away!

Xakir K Moideen February 2016

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"

Answers


Pedro Lobito February 2016

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

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


starkeen February 2016

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]

Post Status

Asked in February 2016
Viewed 3,023 times
Voted 10
Answered 2 times

Search




Leave an answer


Quote of the day: live life