.htaccess - 冲突规则导致内部服务器错误

时间:2014-08-14 10:48:05

标签: apache .htaccess mod-rewrite

我想在我们的网站上添加一个由.htaccess文件强大支持的部分。

目前我有一条处理重写的规则,如果域后面只有一个部分,那么类似下面的内容会导致网站搜索名为“theteam”的页面。

https://very-pc.co.uk/theteam

这很好但我现在正在尝试添加一个具有以下地址的部分:

https://very-pc.co.uk/products

问题是当我为此添加规则(确保它高于先前的规则)时,它会导致500内部服务器错误。如果我有一条规则或另一条规则它完美无缺,但当两条规则都出现时问题就出现了。

这有点难以解释,所以这里是问题代码:

RewriteRule ^products/?$                        ?cat=top-level-summary&page=products [QSA]
RewriteRule ^([^/]+)/?$                         /?cat=generic&page=$1 [QSA]

完整的.htaccess文件:

# Error Handling
ErrorDocument 403                               /index.php?cat=error&page=403
ErrorDocument 404                               /index.php?cat=error&page=404
ErrorDocument 500                               /index.php?cat=error&page=500
ErrorDocument 503                               ?cat=error&page=503

# For security reasons, Option followsymlinks cannot be overridden
Options                                         +FollowSymLinks +SymLinksIfOwnerMatch -        indexes -MultiViews

# Turn the rewrite engine on
RewriteEngine                                   On

# Set the root of the folder to structure
ReWriteBase                                     /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

# Start the mod re-write conditions
# Redirect short URLs
RewriteRule ^broadleaf/?$                       /products/desktops/broadleaf-one    [R=301]
RewriteRule ^broadleafone/?$                    /products/desktops/broadleaf-one    [R=301]
RewriteRule ^broadleaf-one/?$                   /products/desktops/broadleaf-one    [R=301]
RewriteRule ^flex/?$                            /products/desktops/flex             [R=301]
RewriteRule ^fmq/?$                             /fixmequick                         [R=301]
RewriteRule ^warranty/?$                        /productregistration                [R=301]

RewriteCond %{REQUEST_FILENAME}                 !-f
RewriteCond %{REQUEST_FILENAME}                 !-d

# Redirect the specific categories
RewriteRule ^products/?$                        ?cat=top-level-summary&page=products [QSA]
RewriteRule ^([^/]+)/?$                         /?cat=generic&page=$1 [QSA]
RewriteRule ^products/([^/]+)/([^/]+)$          ?cat=product&page=$2 [QSA]
RewriteRule ^products/([^/]+)/?$                /?cat=product-section-summary&page=$1 [QSA]

# Redirects for blog
RewriteRule ^blog/([^/]+)$                      ?cat=blog&page=$1
RewriteRule ^blog/category/([^/]+)$             ?cat=blog_archive_or_category&blog_cat=$1
RewriteRule ^blog/archive/([^/]+)/([^/]+)$      ?cat=blog_archive_or_category&year=$1&month=$2

# Redirect the browser to the https:// version of the domain
RewriteCond %{HTTPS}                            !on
RewriteRule (.*)                                https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

# Disable hot-linking of files on the server
# (other sites cannot link to our images and steal our bandwidth)
RewriteCond %{HTTP_REFERER}                     !^$

#RewriteCond %{HTTP_REFERER}                    !very-pc\.co\.uk [NC]
RewriteCond %{HTTP_REFERER}                     !verypc\.very-dev\.co\.uk [NC]

RewriteCond %{HTTP_REFERER}                     ^http://(cms\.)?very-pc\.co\.uk/ [OR]
RewriteCond %{HTTP_REFERER}                     ^http://(cms\.)?very-dev\.co\.uk/ [NC]
RewriteRule \.(gif|jpg|png|pdf)$                - [F,NC]

# Redirect to remove /index.php and /index.html files
RewriteCond %{THE_REQUEST}                      \ /(.+/)?index\.(php|html)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.(php|html)$           /%1 [NC,R=301,L,QSA]

正如我所说的那样,我试过只有一条线或另一条线存在并且它有效,所以我最好的猜测是这两条规则在某种程度上发生了冲突。

我已经尝试搞乱参数,即将其更改为重定向或添加[L]以使其在第一行符合后停止但我没有运气。

任何建议都会很棒!

1 个答案:

答案 0 :(得分:1)

让你的.htaccess像这样:

# Error Handling
ErrorDocument 403                               /index.php?cat=error&page=403
ErrorDocument 404                               /index.php?cat=error&page=404
ErrorDocument 500                               /index.php?cat=error&page=500
ErrorDocument 503                               ?cat=error&page=503

# For security reasons, Option followsymlinks cannot be overridden
Options +FollowSymLinks +SymLinksIfOwnerMatch -indexes -MultiViews

# Turn the rewrite engine on
RewriteEngine On
# Set the root of the folder to structure
ReWriteBase /

# Disable hot-linking of files on the server
# (other sites cannot link to our images and steal our bandwidth)
RewriteCond %{HTTP_REFERER}                     !^$
RewriteCond %{HTTP_REFERER}                     !verypc\.very-dev\.co\.uk [NC]
RewriteCond %{HTTP_REFERER}                     ^http://(cms\.)?very-pc\.co\.uk/ [OR]
RewriteCond %{HTTP_REFERER}                     ^http://(cms\.)?very-dev\.co\.uk/ [NC]
RewriteRule \.(gif|jpg|png|pdf)$                - [F,NC]

# Redirect the browser to the https:// version of the domain
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Redirect to remove /index.php and /index.html files
RewriteCond %{THE_REQUEST} \s/(.+?/)?index\.(php|html)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.(php|html)$           /%1 [NC,R=301,L,NE]

# Start the mod re-write conditions
# Redirect short URLs
RewriteRule ^broadleaf/?$                       /products/desktops/broadleaf-one    [L,R=301]
RewriteRule ^broadleafone/?$                    /products/desktops/broadleaf-one    [L,R=301]
RewriteRule ^broadleaf-one/?$                   /products/desktops/broadleaf-one    [L,R=301]
RewriteRule ^flex/?$                            /products/desktops/flex             [L,R=301]
RewriteRule ^fmq/?$                             /fixmequick                         [L,R=301]
RewriteRule ^warranty/?$                        /productregistration                [L,R=301]

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

# Redirect the specific categories
RewriteRule ^products/?$ ?cat=top-level-summary&page=products [L,QSA]
RewriteRule ^products/([^/]+)/([^/]+)$ ?cat=product&page=$2 [L,QSA]
RewriteRule ^products/([^/]+)/?$ ?cat=product-section-summary&page=$1 [L,QSA]

# Redirects for blog
RewriteRule ^blog/([^/]+)/$ ?cat=blog&page=$1 [L,QSA]
RewriteRule ^blog/category/([^/]+)$ ?cat=blog_archive_or_category&blog_cat=$1 [L,QSA]
RewriteRule ^blog/archive/([^/]+)/([^/]+)$  ?cat=blog_archive_or_category&year=$1&month=$2 [L,QSA]

RewriteRule ^([^/]+)/?$ /?cat=generic&page=$1 [L,QSA]