Agh
Agh

Reputation: 23

mod_rewrite with directory name matching pattern

I have been dealing for hours with a mod_rewrite problem. I am working with a localhost under Windows. Apache 2.4 with XAMPP

I am trying to do a RewriteRule from my local htaccess with no success. This is the rule:

RewriteRule Event/(.*)$ event/this-event.php?id=$1 [L,R=301]

What I need to rewrite is: Event/4027 to event/this-event?id=4027

The thing is that I have a directory called event so the Event from the pattern is getting confused with the event directory. If I change Event from the pattern to any other word it works fine.

RewriteRule AnyWord/(.*)$ event/this-event.php?id=$1 [L,R=301] # This works!

htaccess file is located in the DOCUMENT_ROOT folder and the content is just:

RewriteEngine On

RewriteBase / 

# and the RewriteRule 

I have tried every tip, trick, advice I have found. I have tried several variations of the rule, using ^Event or (*.)/Event, etc. Also tried using RewriteCond to check if exists file inside directory or if exists directory. I have disabled the mod_speling module just in case the uppercase was disturbing. Also have tried using Options -Multiviews.

I couldn't get none of these to work. If someone can give me an advice I would be very grateful.

Thanks!

Upvotes: 1

Views: 922

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

Your problem is probably that the target path /event/this-event.php matches the pattern Event/(.*)$ and rewrites it to itself.

You need to exclude the target path from rewrite to get this rule to work.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !/event/this-event\.php$
RewriteRule ^Event/(.+)$ event/this-event.php?id=$1 [NC,L,R]

The rewriteCond is important here ,it checks if the request is /event/this-event.php ,skip the rule.

Remove the R Flag from last rule if you want the url not to change in browser.

Upvotes: 1

Simhachalam Gulla
Simhachalam Gulla

Reputation: 998

Try this below code.

RewriteEngine On

RewriteRule event/(.*)$ event/this-event.php?id=$1 [L,QSA]

and the url works for both

http://localhost/project/Event/500
http://localhost/project/event/500

Upvotes: 1

Related Questions