doufendi9063 2015-12-09 04:30
浏览 41
已采纳

目录名称匹配模式的mod_rewrite

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!

  • 写回答

2条回答 默认 最新

  • dougang8233 2015-12-09 05:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?