简体   繁体   English

Apache modrewrite .htaccess-类似链接

[英]Apache modrewrite .htaccess - similar link

I am wondering if it is possible to achieve this bellow: 我想知道是否有可能实现以下目标:

Sometimes second attribute will be submenu 有时第二个属性将是子菜单

RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?menu=$1&submenu=$2 [L,QSA]

Sometimes it will be location 有时会是位置

RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?menu=$1&news=$2 [L,QSA]

But normally, this way it does not work. 但是通常,这种方式不起作用。 Is there any possible way to achieve this? 有什么可能的方法来实现这一目标?

EDIT: Current complete rewrite rule: 编辑:当前完整的重写规则:

RewriteEngine on
ErrorDocument 404 /error

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


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

RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?menu=$1&news=$2 [L,QSA]

RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?menu=$1&submenu=$2 [L,QSA]

RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?menu=$1&submenu=$2&news=$3 [L,QSA]

To elaborate on my comment further you can have rules like this: 要进一步阐述我的评论,您可以使用以下规则:

ErrorDocument 404 /error
RewriteEngine on

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

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

RewriteRule ^([\w-]+)/n-([\w-]+)/?$ index.php?menu=$1&news=$2 [L,QSA]

RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?menu=$1&submenu=$2 [L,QSA]

RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?menu=$1&submenu=$2&news=$3 [L,QSA]

Now your news links should like this: http://domain.com/main-menu/n-sports 现在您的新闻链接应该像这样: http://domain.com/main-menu/n-sports : http://domain.com/main-menu/n-sports

Consider that your regex starts matching \\w , which stands for "word character", usually [A-Za-z0-9_] . 考虑到您的正则表达式开始与\\w匹配, \\w代表“单词字符”,通常为[A-Za-z0-9_] Notice the inclusion of the underscore and digits. 请注意包含下划线和数字。 This also means that this doesn't match accented characters for example. 这也意味着,例如,这与重音字符不匹配。 For sure this don't match the leading slash that begins every path in urls. 当然,这与以urls中的每个路径开头的斜杠不匹配。

You should change your rewrites adding a leading slash: 您应该更改您的重写并添加一个斜杠:

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

RewriteRule ^/([\w-]+)/([\w-]+)/?$ index.php?menu=$1&news=$2 [L,QSA]

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

RewriteRule ^/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?menu=$1&submenu=$2&news=$3 [L,QSA]

I have also commented the third rule because the regex is identical to the second. 我也评论了第三条规则,因为正则表达式与第二条规则相同。 Your approach also have a logical problem, since there is no difference between these lines, so there is no way to call index.php?menu=$1&submenu=$2 您的方法还存在一个逻辑问题,因为这些行之间没有区别,所以无法调用index.php?menu=$1&submenu=$2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM