简体   繁体   English

使用Htaccess重写页面

[英]Rewrite pages using Htaccess

I have htaccess enabled, and below is what I have written so far in. 我启用了htaccess,以下是我到目前为止所写的内容。

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). 第二条规则可以在真正的http服务器主机配置或动态配置文件( .htaccess样式文件)中使用而无需更改。

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. 你给出的最后一个例子, thanks.php / thank-you 不能这样实现,因为“谢谢你”不等于“谢谢”,但我认为这是一个错字。 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: 如果您确实需要该映射,那么您必须为上面的最后一条规则之前放置的单个URI实现一个特殊规则:

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"). 一般提示:您应该始终更喜欢将此类规则放在http服务器主机配置中,而不是使用动态配置文件(“.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). 它们仅作为最后一个选项提供给您无法控制主机配置的情况(阅读:非常便宜的托管服务提供商),或者您有一个依赖于编写自己的重写规则的应用程序(这是一个明显的安全噩梦) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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