简体   繁体   中英

Redirect and Rewrite to remove .html from url

I have a really basic rewrite rule that I've been banging my head against the wall trying to achieve. I have a static HTML site that I want pretty URLs for.

So, I want /something to serve /something.html and I also want to redirect (externally) from /something.html to /something as to not be penalized on SEO for hosting duplicate content.

I don't want to use Multiviews and I don't want to use <rel cannonical="">

This is what I currently have.

RewriteRule ^(.*)\.html$ /$1 [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]

It results in an infinite redirect loop.

It seems to me that the first rule keeps matching even after the requested URL doesn't end in .html.

Yes your rules will cause redirect loop due to use of REQUEST_URI (via RewriteRule ) which changes after application of a rule.

You can use:

    RewriteCond %{THE_REQUEST} \.html [NC]
    RewriteRule ^(.*)\.html$ /$1 [R=302,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html [L]
  • THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM