Apache mod_rewrite不起作用

时间:2012-01-06 15:46:13

标签: php apache mod-rewrite

我正在尝试将请求重定向到http://www.site.com/customer https ://www.site.com/customer并隐藏.php扩展程序,但它无效。这些是我的配置文件:

的httpd.conf:

LoadModule rewrite_module modules/mod_rewrite.so

mysite.conf:

<Directory /var/www/site/customer>
  Order Deny,Allow
  Allow from all
</Directory>
Alias /customer /var/www/site/customer
中的

.htaccess

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}

#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

你错过了什么;)

以下是一些修改:

  • 您忘记了HTTPS规则中的重定向和最后一个标记
  • 您忘记了QSA指令,以便在存在重定向时保留查询字符串

所以在这里,这可能有所帮助,但也许不会100%工作(但它仍然比它更好):

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://$1 [QSA,NC,L]

#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [QSA,R=301,L]

#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [QSA,R=301,L]

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

两个提示:

如果您不在托管环境中(=如果它是您自己的服务器并且您可以修改虚拟主机,而不仅仅是.htaccess文件),请尝试使用RewriteLog指令:它帮助您追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我最喜欢检查regexp的工具:

http://www.quanetic.com/Regex(别忘了选择ereg(POSIX)而不是preg(PCRE)!)