starkeen
starkeen

Reputation: 11

.htaccess mod rewrite 301-redirect

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?

Upvotes: 0

Views: 1137

Answers (3)

eyelidlessness
eyelidlessness

Reputation: 63529

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?

Upvotes: 0

MB.
MB.

Reputation: 7533

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

Upvotes: 2

anonymous
anonymous

Reputation:

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?

Upvotes: 1

Related Questions