Mod_rewrite .htm to fake subdirectory

二次信任 提交于 2020-01-06 06:36:37

问题


I have a website with a CMS that uses mod_rewrite to make URLs look cleaner. Previously, the clean URLs had the extension .htm, but I want to transition this to them appearing as fake subdirectories, IE:

http://www.example.com/pagename/

I have two rewrite rules in place to rewrite both the old scheme and the potential new one:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).htm$ index.php?page=$1 [QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?page=$1 [QSA]

My problem is that the rule I tried to use to redirect old URLs to new ones does nothing.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).htm$ $1/ [R=302,NC,QSA]

回答1:


You can not use two rules with the same pattern as only the first of them will be applied. Try to replace your “rewrite” rule with the new “redirect” rule to get the old URLs redirected instead of just rewritten:

# redirect foo.htm to foo/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.htm$ $1/ [R=301,NC]
# rewrite foo/ to index.php?page=foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ index.php?page=$1 [QSA]


来源:https://stackoverflow.com/questions/2172609/mod-rewrite-htm-to-fake-subdirectory

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