.htaccess with or without slash

社会主义新天地 提交于 2019-12-18 11:51:22

问题


What do I need to do to the following rewrite rule to make it so it works whether or not their is a slash at the end of the URL?

ie. http://mydomain.com/content/featured or http://mydomain.com/content/featured/

RewriteRule ^content/featured/ /content/today.html 

回答1:


Use the $ to mark the end of the string and the ? to mark the preceding expression to be repeated zero or one times:

RewriteRule ^content/featured/?$ content/today.html

But I recommend you to stick to one notation and correct misspelled:

# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]

# add trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ $0/ [L,R=301]



回答2:


simple way to do this :

RewriteEngine On
RewriteBase / 
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC] 


来源:https://stackoverflow.com/questions/540493/htaccess-with-or-without-slash

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