简体   繁体   中英

.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 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]+)/?$ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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