Rewrite rule to add .html suffix

2019-07-25 00:00发布

问题:

I am using WordPress. I need to add a custom rewriterule. I want to have two URLs for a single page.

For example:

www.test.com/abc <--- At present this is implemented & working fine.

www.test.com/abc.html <--- Need to add .html at back side.

I want to allow access to the same page with .html extension.

How can I do that?

回答1:

I explain through comments.

RewriteEngine on
RewriteBase /

# Make sure it's not an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Make sure the hostname is www.test.com
RewriteCond %{HTTP_HOST} ^www\.test\.com

# If the request matches "abc.html", rewrite to abc
RewriteRule ^abc\.html$ abc [L,QSA]

# If you need to make this a generic rule, try this instead:
#RewriteRule ^(.*)\.html$ $1 [L,QSA]

Feel free to drop the L flag, if it's not the last rewrite rule. See RewriteRule Flags for more information on this.

You can test it online: