.htaccess 平面链接的重写规则 [英] .htaccess RewriteRule for Flat Links
问题描述
我对使用 RewriteRule 还很陌生,所以我可能遗漏了一些明显的东西,但我有一个 PHP 脚本,它采用这样的 URL 变量:
I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
我想创建 RewriteRules,以便任何输入此脚本名称/变量组合的人都将其 URL 重写为:
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
同时确保任何输入平面链接 url 的人实际上使用正确的变量名称和值调用脚本.
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
我一直在参考这个链接,我找到了相关主题:
I have been referring to this link and I have found related threads:
Mod_rewrite 麻烦:想从 ?= 直接指向一个平面链接,似乎没有任何效果
但是,我显然做错了什么,因为我无法让这个 URL 以我想要的方式工作.我目前正在使用以下代码,它似乎什么也没做(除了重写 URL 以包含 www,并将对 index.php 的请求重定向到站点根目录):
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
问题显然出在最后一个 RewriteRule 上,假设上面没有任何影响.再说一次,我很可能在做一些可笑的事情.有人可以解释一下我做错了什么吗?
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
感谢您的帮助.
推荐答案
乍一看,您似乎忘记在正则表达式中包含破折号,并且包含了尾部斜杠.改用这个:
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
这篇关于.htaccess 平面链接的重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!