HTACCESS条件和/或重写不起作用

时间:2016-08-04 10:30:53

标签: apache .htaccess mod-rewrite

我的root .htaccess文件存在问题。

这是我的.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# START
RewriteRule ^$ index.php [QSA,L]

# PAGE
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} !category
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [QSA,L]

# POST
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} !category
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ index.php?page=$1&post=$2 [QSA,L]

# CATEGORY
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} category
RewriteRule ^category$ category.php [QSA,L]

# SINGLE CATEGORY
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^category/([A-Za-z0-9_-]+)/?$ category.php?category=$1 [QSA,L]

# XML SITEMAP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap\.xml$ sitemap.php [L]
</IfModule>

当我尝试去

  

www.pixel-factory.at/category

因此我得到一个空的&#34;页面&#34;。这意味着它进入

  

www.pixel-factory.at/index.php?page=category

(不存在)而不是

  

www.pixel-factory.at/category.php

请有人帮忙,不能完成。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以重构条件以缩短代码,在所有地方删除RewriteCond %{HTTP_HOST} category条件并重新排序规则以最终保留通用规则:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /

# XML SITEMAP
RewriteRule ^sitemap\.xml$ sitemap.php [L]

# skip all rules from existing files or directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# CATEGORY
RewriteRule ^category/?$ category.php [NC,L]

# SINGLE CATEGORY
RewriteRule ^category/([\w-]+)/?$ category.php?category=$1 [NC,QSA,L]

# search term
RewriteRule ^search/([\w-]+)/?$ search.php?search=$1 [NC,QSA,L]

# PAGE
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [QSA,L]

# POST
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&post=$2 [QSA,L]