Question

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?

Was it helpful?

Solution

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...

OTHER TIPS

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?

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top