简体   繁体   English

无法在 .htaccess 中使用 Apache mod_rewrite 重写 URL

[英]Unable to rewrite URL using Apache mod_rewrite in .htaccess

On the home page of my website, I have a guide that includes some links, and each redirects to a family guide page, all with the redirection by identification of each element of the guide, remaining as the link在我网站的主页上,我有一个包含一些链接的指南,每个都重定向到一个家庭指南页面,所有重定向都通过识别指南的每个元素进行,保留为链接

https://example.com/family-guide/?id=4

Where '4' is the ID of the element.其中“4”是元素的 ID。

I would like to leave like this:我想这样离开:

https://example.com/family-guide/4

I tried to use mod_rewrite in the .htaccess file:我尝试在.htaccess文件中使用 mod_rewrite:

RewriteRule ^family-guide/([0-9] +)/([0-9a-zA-Z_-]+) family-guide/?Id=$1name=$2 [NC, L]

but nothing happens但什么也没发生

Your rule has some spaces.您的规则有一些空格。 Be careful because if there is a space between the flags [NC, L] the rule is not valid.请注意,因为如果标志[NC, L]之间有空格,则该规则无效。

Another thing is that you want the url https://example.com/family-guide/4 to match a rule and your rule expects two parameters (id and name) while in this url there is only one.另一件事是您希望 url https://example.com/family-guide/4匹配规则,并且您的规则需要两个参数(id 和 name),而在此 url 中只有一个。

I would use a couple of rules instead:我会使用一些规则来代替:

RewriteRule family-guide/([0-9]+)/([0-9a-zA-Z_-]+) family-guide/?id=$1&name=$2 [NC,L]
RewriteRule family-guide/([0-9]+) family-guide/?id=$1 [NC,L]

With this rules if you go to https://example.com/family-guide/4 you should be shown the content of https://example.com/family-guide/?id=4 .有了这个规则,如果你去https://example.com/family-guide/4你应该看到https://example.com/family-guide/?id=4的内容。 And if you go to https://example.com/family-guide/4/whatever it will show you the content of https://example.com/family-guide/?id=4&name=whatever instead, which is what I understood you need.如果你去https://example.com/family-guide/4/whatever它会向你显示https://example.com/family-guide/?id=4&name=whatever的内容,这就是我明白你需要。

Also, the rule order is important as if they were in the opposite order https://example.com/family-guide/4/whatever would match the other rule and show the content of https://example.com/family-guide/?id=4/whatever此外,规则顺序很重要,就好像它们的顺序相反https://example.com/family-guide/4/whatever将匹配其他规则并显示https://example.com/family-guide/?id=4/whatever

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

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