如何修复mod重写

时间:2012-09-16 03:19:02

标签: php forms .htaccess mod-rewrite url-rewriting

我有mod重写的问题

我做了htaccess将url从php转换为html

,一切都很好

但问题是某些文件我不需要转换为form.php

这是我的htaccess

RewriteCond %{REQUEST_URI} !^(.*)form.php(.*)$
RewriteCond %{REQUEST_URI} !^(.*)sitemap\.xml(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^/]+)\.php\s
RewriteRule .* %1.html [R=301,L]
RewriteRule ^([^/]*)\.html$ $1.php
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^.*$ - [L]
RewriteRule ^index.php$ http://%{http_host} [R=301,L]

我不需要转换sitemap.xml和form.php

但是当我尝试查看文件form.php

时出现错误

HTTP错误500(内部服务器错误):服务器尝试完成请求时遇到意外情况。

我能做什么?

1 个答案:

答案 0 :(得分:0)

检查日志是Marc B建议的最佳起点。您应该启用更详细的mod_rewrite日志记录,如下所示:

RewriteLog "/usr/local/var/apache/logs/rewrite.log"
RewriteLogLevel 3

不要将RewriteLogLevel的详细程度高于3。

所以,阅读你的规则,我想我可能知道你的意思。你应该试试这个:

RewriteCond %{REQUEST_URI} !form\.php$

我认为Apache不关心URL后面的查询字符串。除了%{QUERY_STRING}之外,您还可以使用%{REQUEST_URI}变量。除非你有一些可能包含“php”的怪异网址,否则我认为所有网址都以“.php”结尾。

由于“sitemap.xml”似乎没问题,因此您可能应该遵循其示例并使用“\。”以相同的方式转义句点字符(“。”)。

一天后,我有时间考虑这些规则。

# Use simpler rules, not all that jazz you prepend, appended.
# 
RewriteCond %{REQUEST_URI} form\.php$ [OR]
RewriteCond %{REQUEST_URI} sitemap\.xml$ 
# If %{REQUEST_URI} matches either of the previous rule, 
# we skip a certain number of RewriteRules that follow. 
# If you add more rules, and need to skip more, you *need* to adjust this number.
RewriteRule . - [S=2]  

# Your original line reads:
#
# RewriteCond %{THE_REQUEST} ^[A-Z]+\s([^/]+)\.php\s
#
# Using "%{THE_REQUEST}" variable means you are processing
# a string like this
#
# "GET /something.html HTTP/1.1"
#
# Did you really need the HTTP method and the protocol version?
# Do something simpler. Match the the "http://host/foo/bar/baz/" portion
# Then match the first part of the PHP file name. So, if URL ends in "something.php"
# The second parenthesis will match "something". Then append ".html" 

RewriteRule (^.*\/)([^/]+)(\.php)$   $1$2.html [R=301,L]

# **Then** you are trying to rewrite your HTML files to PHP? Why?
# In any case, do something similar as the last RewriteRule. 

RewriteRule (^.*\/)([^/]+)(\.html)$   $1$2.php [R=301,L]

# I do not understand RewriteCond %{ENV:REDIRECT_STATUS} 200 at all
# I don't see you using this environmental variable later on in this
# snippet. Recommend the use of "PT" in case you have other stuff running
# that you want to send the rewrite target to be passed back to the 
# URL mapping engine. 

RewriteRule ^.*$ - [PT, QSA]

# You neglected to add the leading slash in this pattern
RewriteRule ^/index.php$ http://%{http_host} [R=301,L]