.HTACCESS重定向问题

时间:2015-01-06 19:57:57

标签: php .htaccess mod-rewrite

我遇到问题是,在使用.htaccess重写规则从网址中删除.php扩展名后,我再也无法前往目录1级了。

例如,

。如果我导航到www.gameandshame.com/register.php,它会重定向到www.gameandshame.com/register但是,当我使用链接注销时

www.gameandshame.com/auth/login.php它重写为www.gameandshame.com/auth/login然而它产生了404错误,登录和注销文件位于auth文件夹内,这非常重要我保持我的网站文件有序正确的,所以我不想把它们拉到我的主目录。

有人知道他们为什么会产生404错误吗?

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

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.gameandshame.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.gameandshame.com/$1 [R=301,L]


# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]


#Handle Profile Re-directs
RewriteRule ^profile/?$ profile.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*)/?$ profile.php?u=$1 [NC,L]

#Handle Post re-directs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/?$ post.php [NC,L]
RewriteRule ^post/(.*)/?$ post.php?postid=$1 [NC,L]

1 个答案:

答案 0 :(得分:1)

在root .htaccess中有这些规则:

Options -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} ^gameandshame\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.gameandshame.com/$1 [R=301,L,NE]

## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/(.+)/?$ profile.php?u=$1 [NC,L,QSA]

#Handle Post re-directs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/(.+)/?$ post.php?postid=$1 [NC,L,QSA]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]