如果以上条件都不匹配,则Htaccess rewriteRule

时间:2016-01-12 07:44:42

标签: php apache .htaccess mod-rewrite url-rewriting

我有以下htaccess,我正在尝试重写所有不符合任何规则的网址。

Options -Indexes
Options +FollowSymLinks
RewriteEngine On

# —————— rule1 ——————
RewriteCond %{REQUEST_URI} ^(.*)/learn/(why|about)$
RewriteRule ^(.*)/(.*)$ ./index.php?page=$2 [L,QSA]

# —————— rule2 ——————
RewriteCond %{REQUEST_URI} ^(.*)/profile/edit$
RewriteRule ^(.*)$ ./index.php?page=profile [L,QSA]

# —————— rule3 ——————
RewriteCond %{REQUEST_URI} ^(.*)/users/(.*)$
RewriteRule ^(.*)/(.*)$ ./index.php?page=user&userUrl=$2 [L,QSA]

我想要的是这样的东西

RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule ^(.*)$ ./index.php?page=404 [L,QSA]

但它似乎不起作用。有什么方法可以实现吗?感谢。

1 个答案:

答案 0 :(得分:1)

你的最后一个包罗万象的规则是这样的:

DirectoryIndex index.php
Options +FollowSymLinks -Indexes
RewriteEngine On

# —————— rule1 ——————
RewriteCond %{REQUEST_URI} ^(.*)/learn/(why|about)$
RewriteRule ^(.*)/(.*)$ index.php?page=$2 [L,QSA]

# —————— rule2 ——————
RewriteCond %{REQUEST_URI} ^(.*)/profile/edit$
RewriteRule ^(.*)$ index.php?page=profile [L,QSA]

# —————— rule3 ——————
RewriteCond %{REQUEST_URI} ^(.*)/users/(.*)$
RewriteRule ^(.*)/(.*)$ index.php?page=user&userUrl=$2 [L,QSA]

# —————— rule4 ——————
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php?page=404 [L,QSA]