I have static html files and I want to be able to remove the .html
file extension at the end of the url inside the address bar.
So instead of /contact-us.html
- I want it to display as this /?contact-us
One thing to notice here is there is a question mark before the page name(s). I want to do this to any html page not just contact-us.html
. How can I achieve this?
You can rewite the single page in your .htaccess using this:
RewriteEngine On
RewriteRule ^/contact-us.html$ /?contact-us [NC,L]
More specifically, if you want to rewrite all .html extensions you can use a regular expression to match a pattern of pages:
RewriteEngine On
RewriteRule ^(.*)(.html)$ /?$1 [NC,L]
In the above example, it will match any .html extension. eg /example-page.html will be /?example-page
You can test it out at an htaccess tester here: http://htaccess.mwl.be/
However , question mark (?) is reserved for query strings, and your rewrites may end up in an infinite loop because the rule will rewrite http://www.example.com/example-page.html to http://www.example.com/?example-page , which the server will resolve to http://www.example.com/index.html?example-page , which in turn triggers another re-write, ad infinitum.
A better approach would be to mock a directory structure, and re-write /example-page.html to /example-page/ This gives you some SEO benefits as well, and can be done with this .htaccess:
RewriteEngine On
RewriteRule ^(.*)(.html)$ /?$1/ [NC,L]
If you want to be able to use folder path instead of .html you need these rules.
This allows you to use http://domain.com/contact-us
in the address bar.
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+(.+)\.html [NC]
RewriteRule ^ %1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/?$ $1.html [L]
Add this to your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
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.