我在尝试设置正确的htaccess文件时遇到问题。
我基本上想要做的是实现干净的URL并隐藏除一个文件之外的.php扩展名。
我目前设置的内容如下:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
#RewriteRule ^(?!recruit)(.*)$ $1.php [NC,L]
第一条规则将在“招募”之后采取任何措施,并将其作为网址中的获取变量传递
www.example.com/recruitHI - > www.example.com/recruit.php?id=HI
另一条规则需要做的是将.php附加到除了以招募开头的任何内容之外的任何其他内容。
www.example.com/index - >将寻找index.php
www.example.com/contact - >将寻找contact.php
www.example.com/recruit - >由于第一条规则需要被忽略
当我有2个规则并启动apache时,我收到错误,说我的配置错误。他们都是单独工作。
答案 0 :(得分:1)
在开头指定RewriteBase
。如果所有内容都在站点根目录中,那么
RewriteBase /
否则,您的所有规则都应以^/
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ $1.php [NC,L]
由于您拥有L
recruit
之后的规则只会影响没有它的项目。但是,不应该为每个url可能性设置脚本,而应该使用单个Front Controller。
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ index.php [NC,L]
然后您可以使用FallBack提供程序(在Apache中更新)
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
FallbackResource /index.php
答案 1 :(得分:1)
您可以使用:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
第二条规则只会为非文件或目录且已经是有效的php文件的URI添加.php
扩展名。
答案 2 :(得分:1)
尝试向第二条规则添加条件,以便它不会盲目地将php
附加到URI的末尾:
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]