apache mod_rewrite和.php文件扩展

时间:2013-01-08 16:59:06

标签: php apache mod-rewrite

我遇到了mod_rewrite的问题。我有一个基本的php网站,其中所有文件都有.php扩展名。

我想要发生的是:

www.site.com/file 和www.site.com/file /

都提供www.site.com/file.php,但没有显示扩展名(因此URL栏显示为www.site.com/file)

如果有人在www.site.com/file.php中输入,则会重定向到www.site.com/file

我的.htaccess代码如下:

Options +FollowSymLinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^([^/]+)/$ /$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]

RewriteRule ^([^/.]+)$ $1.php [L]

# Rules For file inside subfolders like
# /folder/myfile --> /folder/myfile.php
# and
# /folder/myfile/ --> /folder/myfile
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/([^/]+)/$ /$1/$2 [R=301,L]
RewriteRule ^(.+)/([^/.]+)$ $1/$2.php [L]

前两个(/ file和/ file / serving file.php)工作正常,但如果我输入www.site.com/file.php,浏览器会重定向到:

http://www.site.com/home/siteuser/public_html/file

并发出404错误

重写日志显示以下内容(loglevel 2):

[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.com/public_html/] rewrite 'about-us.php' -> 'about-us'
[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.com/public_html/] explicitly forcing redirect with http://www.mysite.com/home/mysite.com/public_html/about-us
[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.com/public_html/] escaping http://www.mysite.com/home/mysite.com/public_html/about-us for redirect
[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.com/public_html/] redirect to http://www.mysite.com/home/mysite.com/public_html/about-us [REDIRECT/301]
[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.com/public_html/] rewrite 'home/mysite.com/public_html/about-us' -> 'home/mysite.com/public_html/about-us.php'
[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.com/public_html/] strip document_root prefix: /home/mysite.com/public_html/home/mysite.com/public_html/about-us.php -> /home/mysite.com/public_html/about-us.php
[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.com/public_html/] internal redirect with /home/mysite.com/public_html/about-us.php [INTERNAL REDIRECT]
[08/Jan/2013:17:35:33 +0000] [www.mysite.com/sid#b970c168][rid#b991e190/initial/redir#1] (1) [perdir /home/mysite.com/public_html/] pass through /home/mysite.com/public_html/home
[08/Jan/2013:17:35:34 +0000] [www.mysite.com/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.com/public_html/] pass through /home/mysite.com/public_html/favicon.ico

如何阻止它重定向到文件路径,并将其重定向到www.site.com/file?

0 个答案:

没有答案