如何?虚荣的URL和放大器;忽略PHP扩展与的.htaccess放大器、虚荣、htaccess、URL

2023-09-02 09:38:31 作者:巴黎塔下旧少年

我有以下的.htaccess正常使用:

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME -f [OR]
的RewriteCond%{} REQUEST_FILENAME -d
重写规则。*  -  [L]
重写规则^(。*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
 

这将允许我这样写网址:

  http://www.mysite.com/peter => http://www.mysite.com/mypage.php?u=peter
 

但是,如果我想有一个aditional的规则,以避免编写PHP扩展现有的文件吗?

  http://www.mysite.com/contact(contact.php存在于文件)
 
PHP中的性能优化利器 php生成器的详解

我尝试添加这对我CURR htaccess的(但不工作):

 的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_FILENAME}  PHP -f
重写规则^(。*)$ $ 1.PHP
 

换句话说,我想有虚荣心的URL,而忽略了PHP扩展在相同的.htaccess。

解决了! 由于乌尔里希Palha和马里奥(不知道有关的顺序和最后一个[L]标志!

  RewriteEngine叙述上
#除非目录,删除尾随斜线
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^ /] +)/ $ http://www.mysite.com/$1 [R = 301,L]

#重定向外部的.php请求扩展名的URL
的RewriteCond%{THE_REQUEST} ^(。+)。PHP([#?] [^ ] *)? HTTP /
重写规则^(。+)。PHP $ http://www.mysite.com/$1 [R = 301,L]

的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_FILENAME}  PHP -f
重写规则^(。*)$ $ 1.PHP [L]

的RewriteCond%{} REQUEST_FILENAME -f [OR]
的RewriteCond%{} REQUEST_FILENAME -d
重写规则。*  -  [L]
重写规则^(。*)$ http://www.mysite.com/mypage.php?u=$1 [NC]
 

解决方案

这是排序的问题。规则集的应用将以它们将记录在的.htaccess 订单 请参阅Everything你曾经想知道的关于mod_rewrite规则,但不敢问?

只需添加新模块作为第一个重写的语句,添加一个 [尾页] 标记,它应该工作。

此规则无条件地(不被任何的RewriteCond守卫),因此应始终是最后所有的规则集的:

 重写规则^(。*)$ ...
 

(和所有preceeding的规则集应该preferrably携带 [L] 标记。你有一点要解决此问题的存在。)

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]

解决方案

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:

 RewriteRule ^(.*)$ ...

(And all preceeding RewriteRules should preferrably carry a [L] flag. You have a bit of a workaround there.)