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条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式