.htaccess将.php附加到URL

时间:2015-02-10 15:10:28

标签: php apache .htaccess

我使用以下代码(特别是RewriteRule ^s/([a-zA-Z0-9\-]+)/?$ /index.php?s=$1 [L,QSA])的意图是针对URL

example.com/s/full-name

像工作一样

example.com/index?s=full-name

目前,我的服务器允许我使用网址式example.com/s/$1,但它不会显示可在index?s=full-name找到的内容。相反,它将URL更改为

example.com/s/full-name.php/

请求example.com/s/full-name时我不明白为什么。我非常感谢任何建议。

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^s/([a-zA-Z0-9\-]+)/?$ /index.php?s=$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]

Options -Indexes

ErrorDocument 400 /index?err=400
ErrorDocument 401 /index?err=401
ErrorDocument 403 /index?err=403
ErrorDocument 404 /index?err=404 
ErrorDocument 500 /index?err=500

1 个答案:

答案 0 :(得分:0)

首先,index routing命令中的条件有误。第二件事是你有一个规则,将php附加到你的请求的末尾,但它没有检查是否存在php文件。尝试重新排序规则并修复条件:

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^s/([a-zA-Z0-9\-]+)/?$ /index.php?s=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)/?$ $1.php [NC,L]
相关问题