Trailing slash causes 404, can I fix using htaccess?

坚强是说给别人听的谎言 提交于 2020-01-02 04:48:15

问题


The offending URLs are:

  • (Doesn't work) http://alltheragefaces.com/face/surprised-wut/
  • (Works) http://alltheragefaces.com/face/surprised-wut

The .htaccess rule I have for these types of URLs looks like:

RewriteRule ^face/(.*)$ face.php?term=$1

What can I do to make both of these URLs to go to the same page?


回答1:


You can use this:

 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.+)/$ /$1 [L,R=301]

The first line says: "if it's not a directory" (because then a trailing slash would have meaning). The second line says: redirect everything from start to the trailingslash and end to everything that was in there, without the trailing slash.

Put your own RewriteRule in there (below that one, not above) so your normal redirect still works after the trailing slash was removed.

(this one will obviously work for /body/ too, and not only for /face/.




回答2:


I use this rule which is slightly modified to maintain any subfolder structure

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]


来源:https://stackoverflow.com/questions/10346944/trailing-slash-causes-404-can-i-fix-using-htaccess

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