开发者

.htaccess RewriteRule for Flat Links

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

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

As well as ensuring that anyone who types in the flat-link url, actually calls the开发者_如何学JAVA script with the right variable name and value.

I have been referring to this link and I have found related threads:

Mod_rewrite flat links

Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work

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

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?

Thanks for your help.


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


If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜