如何?虚荣的URL和放大器;忽略PHP扩展与的.htaccess [英] How-to? Vanity URL & ignore .PHP extension with .htaccess

查看:238
本文介绍了如何?虚荣的URL和放大器;忽略PHP扩展与的.htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的.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存在于文件)
 

我尝试添加这对我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
订单 请参阅<一href="http://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-as">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.)

这篇关于如何?虚荣的URL和放大器;忽略PHP扩展与的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆