简体   繁体   English

具有目录名称匹配模式的mod_rewrite

[英]mod_rewrite with directory name matching pattern

I have been dealing for hours with a mod_rewrite problem. 我已经处理了几个小时的mod_rewrite问题。 I am working with a localhost under Windows. 我正在Windows下使用本地主机。 Apache 2.4 with XAMPP 带有XAMPP的Apache 2.4

I am trying to do a RewriteRule from my local htaccess with no success. 我正在尝试从本地htaccess执行RewriteRule,但没有成功。 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 我需要重写的是:Event / 4027到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. 关键是我有一个名为event的目录,因此模式中的Event与event目录混淆了。 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: htaccess文件位于DOCUMENT_ROOT文件夹中,其内容仅为:

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. 我已经尝试过使用^ Event或(*。)/ Event等规则的几种变体。还尝试过使用RewriteCond检查目录中是否存在文件或目录是否存在。 I have disabled the mod_speling module just in case the uppercase was disturbing. 我已经禁用了mod_speling模块,以防大写干扰。 Also have tried using Options -Multiviews. 还尝试使用选项-Multiviews。

I couldn't get none of these to work. 我无法让这些工作。 If someone can give me an advice I would be very grateful. 如果有人可以给我建议,我将不胜感激。

Thanks! 谢谢!

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

Your problem is probably that the target path /event/this-event.php matches the pattern Event/(.*)$ and rewrites it to itself. 您的问题可能是目标路径/event/this-event.php匹配模式Event /(.*)$并将其重写为自身。

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. rewriteCond在这里很重要,它检查请求是否为/event/this-event.php,跳过规则。

Remove the R Flag from last rule if you want the url not to change in browser. 如果您不想在浏览器中更改网址,请从最后一条规则中删除R标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM