简体   繁体   中英

Rewrite pages using Htaccess

I have htaccess enabled, and below is what I have written so far in.

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

I would like to apply clean url to my pages for instance, I would like

terms.php to be rewritten as /terms-and-conditions
privacy.php /privacy
thanks.php / thank-you

You can try with following:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^terms-and-conditions terms.php
RewriteRule ^privacy privacy.php
RewriteRule ^thank-you thanks.php

This is probbly what you are looking for, though you may have to tweak it for your usage:

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ $1.php [L]

That second rule can be used without change in the real http servers host configuration or in dynamic configuration files ( .htaccess style files).

Obviously the rewriting module has to be enabled.


The last example you give, thanks.php / thank-you can not be implemented this way, since "thank-you" does not equal "thanks", but I assume that is a typo. If you really need that mapping, then you have to implement a special rule just for that single URI placed before that last rule above:

RewriteRule ^/?thank-you$ thanks.php [L]

And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

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