mod_rewrite规则不起作用

时间:2015-07-24 10:47:58

标签: .htaccess mod-rewrite

我想用mod_rewrite .htaccess:

这样做
myurl.com/category/sub/title + optionally (/en-GB for language)
myurl.com/category/ID (number)
myurl.com/content/title (Letters with - & _)

/ sub可以选择

with:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ index.php?page=$1&sub_page=$2&tree_page=$3 [QSA]
</IfModule>

有时,URL没有子类别,只是类别。

但是,第一个代码无效。如何实现上述所有3个选项?

1 个答案:

答案 0 :(得分:0)

不是试图构建一个接受所有这一切的复杂正则表达式,而是采用更专业的规则:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_]+)$ index.php?page=$1 [L, QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)$ index.php?page=$1&sub_page=$2 [L, QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ index.php?page=$1&sub_page=$2&tree_page=$3 [L, QSA]
</IfModule>

请注意,我没有测试过这个。它只是为了指出你正确的方向......