0

我在使用 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 错误

重写日志显示以下内容(日志级别 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?

4

0 回答 0