
Question:
I want: all links which not contained filename (not .html, .jpg, .png, .css) redirect with state 301 to directory, for example: <a href="http://mysite.com/article" rel="nofollow">http://mysite.com/article</a> -> <a href="http://mysite.com/article/" rel="nofollow">http://mysite.com/article/</a> But <a href="http://mysite.com/article/article-15.html" rel="nofollow">http://mysite.com/article/article-15.html</a> not redirects. What regulat expression I must write to .htaccess for adding slash to virtual directories?
Answer1: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...
Answer2:Clarification needed:
Given the url: <a href="http://server/path/file" rel="nofollow">http://server/path/file</a>
Does that get redirected to: <a href="http://server/path/" rel="nofollow">http://server/path/</a>
Or does it get redirected to: <a href="http://server/path/file/" rel="nofollow">http://server/path/file/</a>
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?
Answer3: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?