怎么做?虚荣 URL 和忽略带有 .htaccess 的 .PHP 扩展名
我已经让以下 .htaccess 完美工作:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
这将允许我编写这样的 url:
http://www.mysite.com/peter => http://www.mysite.com/mypage.php?u=peter
但是,如果我想要有一个附加规则来避免为现有文件编写 .php 扩展名,该怎么办?
http://www.mysite.com/contact (contact.php exists on file)
我尝试将其添加到我的 curr htaccess 中(但不起作用):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
换句话说,我希望拥有虚荣网址并忽略同一 .htaccess 中的 .php 扩展名。
解决了! 感谢 Ulrich Palha 和 Mario(不知道顺序和 LAST [L] 标志!
RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
I've got the following .htaccess working perfectly:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
This will allow me to write urls like this:
http://www.mysite.com/peter => http://www.mysite.com/mypage.php?u=peter
But what if I want have an aditional rule to avoid writing the .php extension for existing files?
http://www.mysite.com/contact (contact.php exists on file)
I tried adding this to my curr htaccess (but doesnt work):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
In other words, I would like to have vanity urls and ignore the .php extension in the same .htaccess.
SOLVED!
Thanks to Ulrich Palha and Mario (didnt know about the ordering and the LAST [L] flag!
RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个订购的问题。 RewriteRules 按照 .htaccess 中标注的顺序应用
请参阅所有内容您曾经想了解 Mod_Rewrite 规则但又不敢问吗?
只需添加新块作为第一个重写语句,添加
[LAST]
标志,它就应该可以工作。该规则无条件工作(不受任何 RewriteCond 保护),因此应始终是所有 RewriteRules 的最后一个:(
并且所有前面的 RewriteRules 最好应带有
[L]
标志。您有一个解决方法那里。)It's a matter of ordering. RewriteRules are applied in the order they are noted in the .htaccess
See Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?
Just add your new block as first rewrite statement, add a
[LAST]
flag, and it should work.This rule works unconditionally (not guarded by any RewriteCond), so should always be the last of all RewriteRules:
(And all preceeding RewriteRules should preferrably carry a
[L]
flag. You have a bit of a workaround there.)对我来说,这段代码有效:
在“/etc/apache2/sites-available/default”或“httpd.conf”中,
您必须(至少)在默认目录
或在站点特定站点目录中
,并在站点目录中创建或编辑“.htacces”(例如“/var/www/my-site/.htaccess”)并添加这段代码:
For me this code work:
in "/etc/apache2/sites-available/default" or "httpd.conf"
you must have (at least) these options in the default directory
OR in the site specific site directory
and create or edit the ".htacces" in the directory of the site (for example "/var/www/my-site/.htaccess") and add this code: