奇怪的Mod_Rewrite行为

时间:2016-09-06 21:01:36

标签: .htaccess mod-rewrite

我已经在这里查看了几个mod_rewrite问题,但他们建议的任何内容都没有。迄今为止。

我有以下规则:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]*)/$ ?action=$1 [L]

## General Pages
RewriteRule ^privacy-policy/$ ?action=privacy-policy [L]
RewriteRule ^terms-conditions/$ ?action=terms-conditions [L]
RewriteRule ^about/$ ?action=about [L]
RewriteRule ^contact-us/$ ?action=contact-us [L]
RewriteRule ^sitemap.xml$ ?action=sitemap [L]

## Blog Pages
RewriteRule ^blog/$ ?action=blog [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ ?action=blog&category=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ ?action=blog&category=$1&id=$2&title=$3 [L]

第一个blog规则正常,第三个blog规则正常。

第二个blog规则无法正常运行,假设我有以下网址:

/blog/general/

第二个blog规则返回blog/general作为category的值不正确,它应该只返回general作为category的值

此问题仅发生在我的生产环境中,在我的本地开发环境中不会发生。

我真的很难过这个,任何建议都非常感激。

1 个答案:

答案 0 :(得分:1)

尝试重新排序规则并将SSLwww规则合并,以避免多次重定向(对SEO和客户体验不利):

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

## General Pages
RewriteRule ^privacy-policy/$ ?action=privacy-policy [L]
RewriteRule ^terms-conditions/$ ?action=terms-conditions [L]
RewriteRule ^about/$ ?action=about [L]
RewriteRule ^contact-us/$ ?action=contact-us [L]
RewriteRule ^sitemap.xml$ ?action=sitemap [L]

## Blog Pages
RewriteRule ^blog/$ ?action=blog [L,QSA]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ ?action=blog&category=$1 [L,QSA]
RewriteRule ^blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ ?action=blog&category=$1&id=$2&title=$3 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]*)/$ ?action=$1 [L,QSA]