Mod_rewrite isn't working as intended

喜夏-厌秋 提交于 2019-12-13 03:58:46

问题


I'm trying to rewrite a specific structured url to another structure.

The original url is

http://www.mywebsite.com/FF_Supersneaker_Black_Metallica_p/155-157.htm

and I am trying to rewrite it to

http://www.mywebsite.com/FF_Supersneaker_Black_Metallica.html

essentially removing "_p/155-157.htm" and appending .html

The htaccess file I am using to do this is

<IfModule mod_rewrite.c>  
    RewriteEngine On    
    RewriteRule /(.*)/(.*)_p/[^/]*.htm$ /$1$2.html
</IfModule>

But it seems to be failing, Here is the rewrite log I get when going to the page.

255.255.255.255 - - [25/Apr/2012:12:34:43 --0400] [www.mywebsite.com/sid#9b25ed8][rid#9e33c28/initial] (3) [perdir /var/www/test/] strip per-dir prefix: /var/www/test/FF_Supersneaker_Black_Metallica_p/155-157.htm -> FF_Supersneaker_Black_Metallica_p/155-157.htm
255.255.255.255 - - [25/Apr/2012:12:34:43 --0400] [www.mywebsite.com/sid#9b25ed8][rid#9e33c28/initial] (3) [perdir /var/www/test/] applying pattern '/(.*)/(.*)_p/[^/]*.htm$' to uri 'FF_Supersneaker_Black_Metallica_p/155-157.htm'
255.255.255.255 - - [25/Apr/2012:12:34:43 --0400] [www.mywebsite.com/sid#9b25ed8][rid#9e33c28/initial] (1) [perdir /var/www/test/] pass through /var/www/test/FF_Supersneaker_Black_Metallica_p/155-157.htm

I believe it seems like something is wrong with my rule, but I'm not sure what. If anyone can give me some thoughts, I'd appreciate it.

Thank you for any help.


回答1:


First, look at this:

strip per-dir prefix: /var/www/test/FF_Supersneaker_Black_Metallica_p/155-157.htm -> FF_Supersneaker_Black_Metallica_p/155-157.htm

That path you're matching against is actually FF_Supersneaker_Black_Metallica_p/155-157.htm because you're running inside a directory context (from a .htaccess file). So it's not going to match anything starting with /...

That suggests that what you really want is simply...

RewriteRule (.*)_p/[^/]*.htm$ /$1.html

...if you want the result to be:

FF_Supersneaker_Black_Metallica.html


来源:https://stackoverflow.com/questions/10320029/mod-rewrite-isnt-working-as-intended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!