htaccesc重写规则不起作用

时间:2017-08-19 12:40:50

标签: .htaccess mod-rewrite

<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch 
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [L]

origin url:example.com/index.php?lang=de&menu=1&submenu=0 新网址:example.com/de/1/0

新网址加载主页,但需要另一个页面

1 个答案:

答案 0 :(得分:0)

<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch 
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [L]

如果您遵循这一点,您告诉Apache将文件系统中的所有文件或目录都删除,并将其重写为index.php。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]

这就是它正在做的事情。

为了使你做到你想要的,你必须这样做:

<Files ~ (\.php)>
Options +SymLinksIfOwnerMatch 
</Files>
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
# if file/director does not exist, and if it matches the pattern
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/([0-9]+)$ index.php?lang=$1&menu=$2&submenu=$3 [QSA,L]
# then if it did not match the pattern, send it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]

请注意,您没有提供足够的信息来了解您实际尝试实现的目标。但我的猜测是这样的。我还假设您要将值附加到index.php(QSA)的查询字符串中,这通常是您想要的。

但是在你指定所有网址应该做的事情之前,你不可能进一步猜测。

请注意,通过匹配任何非文件/目录,并且在没有进一步参数的情况下重写它作为index.php(L)的最后一个命令,结束请求处理,它将永远不会遇到第二种情况。