.htaccess mod rewrite 301-redirect

做~自己de王妃 提交于 2019-12-25 04:38:09

问题


I want: all links which not contained filename (not .html, .jpg, .png, .css) redirect with state 301 to directory, for example: http://mysite.com/article -> http://mysite.com/article/ But http://mysite.com/article/article-15.html not redirects. What regulat expression I must write to .htaccess for adding slash to virtual directories?


回答1:


I think the following might work:

RewriteEngine on   
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

When it comes to mod_rewrite I can never be sure without testing though...




回答2:


Clarification needed:

Given the url: http://server/path/file

Does that get redirected to: http://server/path/

Or does it get redirected to: http://server/path/file/

As in: Do you want the redirects to go to the parent path, or do you just want to add a slash and assume directory out of the current path?




回答3:


MB's RewriteRule above will fail on paths like /a because it needs to match at least two characters after the slash. Moreover it only matches on top directory URLs.

RewriteRule ^(([^\/]+\/)*[^\/\.]+)$ http://%{HTTP_HOST}/$1/ [R=301,L]

Is the purpose of this to reduce history pollution/false negatives?



来源:https://stackoverflow.com/questions/81631/htaccess-mod-rewrite-301-redirect

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