开发者

Using ModRewrite to get rid of extentions

开发者 https://www.devze.com 2022-12-14 03:09 出处:网络
I would like to get开发者_开发技巧 rid of all the file extensions on my site. Except when they are on the index i would like it to say nothing...

I would like to get开发者_开发技巧 rid of all the file extensions on my site. Except when they are on the index i would like it to say nothing...

change this foo.com/index.html

to this foo.com/

and when the user goes to another page like foo.com/contact-us.html

it will be foo.com/contact-us

RewriteEngine On
RewriteRule ^ this is where i get confused :(

Thanks in advance!


Try these rules:

RewriteRule ^index\.html$ / [L,R=301]
RewriteRule (.+)\.html$ /$1 [L,R=301]


I'd use a slightly different method:

RewriteCond %{REQUEST_FILENAME}\.html -f # if request.html exists and is a file
RewriteRule ^(.+)$ $1\.html [QSA] # then rewrite to that file.


It doesn't have to be so complicated, apache has an option to accomplish what you want:

Just add this rule to your .htaccess and /contact-us/ will automatically be rewritten to contact-us.html:

Options MultiViews

That's it!

0

精彩评论

暂无评论...
验证码 换一张
取 消