Htaccess重写规则可以在localhost中运行,但只能在服务器上部分工作

时间:2016-05-13 13:53:15

标签: .htaccess mod-rewrite url-rewriting

我遇到了一个问题,我的htaccess重写命令在localhost上运行,但在上传到服务器时它们部分工作。

Options +FollowSymLinks
Options -Multiviews
#Turn Rewrite Engine on
RewriteEngine on
#rewrite for pages
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteRule ^about about.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^privacy privacy.php [NC,L]
RewriteRule ^terms terms.php [NC,L]
RewriteRule ^contact contact.php [NC,L]
RewriteRule ^submit submit.php [NC,L]
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?article_id=$1&title=$2 [NC,L]

以下所有单一重写规则在localhost和服务器上都可以正常

RewriteRule ^about about.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^privacy privacy.php [NC,L]
RewriteRule ^terms terms.php [NC,L]
RewriteRule ^contact contact.php [NC,L]
RewriteRule ^submit submit.php [NC,L]

当我尝试使用url查询字符串重写规则时,它似乎无法在服务器上运行但在localhost中正常工作:

RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?article_id=$1&title=$2 [NC,L]

我试图通过仅查询id来尝试使其工作,但无济于事:

RewriteRule ^article/([0-9]+) article.php?article_id=$1 [NC,L]

我正在寻找的最终结果是www.website.com/article/123/article-title

2 个答案:

答案 0 :(得分:1)

问题在于你的前两个条件是否适用于不同的规则,

变化

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

答案 1 :(得分:0)

您能否一次尝试以下规则

规则1

RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ http://%{HTTP_HOST}/article.php?article_id=$1&title=$2 [NC,L]

规则2

RewriteRule ^/article/([0-9]+)/([0-9a-zA-Z_-]+)$ http://%{HTTP_HOST}/article.php?article_id=$1&title=$2 [NC,L]

请求链接www.website.com/article/123/article-title。 如果这些都不起作用。然后将两个规则的标志从[NC,L]更改为[L,R=301](仅用于测试目的)并告诉我在两种情况下请求URL后浏览器地址栏显示的内容(在此处我已从内部重定向更改)到外部重定向。所以重定向的地址应该在浏览器地址栏中。)

更新

Options +FollowSymLinks
Options -Multiviews
#Turn Rewrite Engine on
RewriteEngine on
#rewrite for pages
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ http://%{HTTP_HOST}/article.php?article_id=$1&title=$2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^privacy privacy.php [NC,L]
RewriteRule ^terms terms.php [NC,L]
RewriteRule ^contact contact.php [NC,L]
RewriteRule ^submit submit.php [NC,L]