简体   繁体   English

单页.htaccess重写

[英]Single Page .htaccess Rewrite

I want to use htaccess rewrite to display single pages without the extensions. 我想使用htaccess重写来显示没有扩展名的单个页面。

http://site.com/account to http://site.com/account.php http://site.com/accounthttp://site.com/account.php

RewriteEngine on
RewriteRule ^account$ /account.php [L]

Shouldn't this work? 这不行吗? It does nothing. 它什么也没做。

I also tried a general one, but that didn't work either: 我也尝试了一个普通的方法,但是那也不起作用:

RewriteEngine on
RewriteRule ^(.*)/$ $1.php

My recommendation is to use dynamic rules, instead of static rules: 我的建议是使用动态规则,而不是静态规则:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

This way any page will refer to any PHP script. 这样,任何页面都可以引用任何PHP脚本。

For instance: 例如:

http://site.com/account will load http://site.com/account.php http://site.com/account将加载http://site.com/account.php

http://site.com/login will load http://site.com/login.php http://site.com/login将加载http://site.com/login.php

If you do prefer to use static rewrite rules however, then you can use the following: 但是,如果您确实喜欢使用静态重写规则,则可以使用以下内容:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^index index.php [L]

I would like to specify that if you are planning on using these rewrite rules for scripts, located inside a subfolder, like 我要指定的是,如果您打算对位于子文件夹内的脚本使用这些重写规则,例如

http://site.com/myfolder/account http://site.com/myfolder/account

then it would be best to add a RewriteBase after you initiate RewriteEngine 那么最好在启动RewriteEngine之后添加RewriteBase

is the rewrite module enabled? 重写模块是否启用? Maybe try checking for URL ending with slash. 也许尝试检查以斜杠结尾的URL。

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^account\/\?$ /account.php [L]
</IfModule>

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

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