简体   繁体   中英

RedirectMatch or Rewrite Rule for 301 Redirects of *.html to non-extension path

I've searched everywhere, including SO, can't quite find the answer to this question:

I'd like to set up a 301 redirect such that:

www.mysite.com/some-page.html

redirects to

www.mysite.com/some-page/

I'm using the below in my htaccess file (it's a WordPress site):

RewriteEngine On
RedirectMatch 301 (.*)\.html$ $1

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

The problem
If I visit www.mysite.com/ , it redirects to www.mysite.com/index

(Note: If I visite www.mysite.com/some-page.html , it does work and redirects to www.mysite.com/some-page/ )

Any advise on how to make this work? I'm sure I'm missing something obvious...

The RedirectMatch is part of mod_alias, and the RewriteRule stuff is part of mod_rewrite. Both modules will get applied to the same URI at different points of the processing pipeline. That means you can't just tell apache to stop what it's doing and redirect. The mod_rewrite part will still get applied regardless.

To deal with this, just stick with using mod_rewrite instead:

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

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

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