简体   繁体   中英

Using mod_rewrite / RewriteRule to create SEO friendly URLs

I'm trying to get this unfriendly link:

http://test.mysite.com/featured1new.php?homedetails=12-Magnolia-Court-Branson-MO-65616&ID=11315943&PHOTOID=20140401022411163178000000

To display as this friendly link when using my website:

http://test.mysite.com/homedetails/12-Magnolia-Court-Branson-MO-65616/ID/11315943/PHOTOID/20140401022411163178000000

This is the code in my .htaccess file in the main directory:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule homedetails/(.*)/ID/(.*)/PHOTOID/(.*)/ featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3
RewriteRule homedetails/(.*)/ID/(.*)/PHOTOID/(.*) featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3

The problem is the link still displays the old way in the website, however, if I key the new RewriteRule way directly into the browser, it works fine. So part of this is working the way I need it too.

Am I supposed to change my html code in the website to match/use the RewriteRule? (After reading on this site for quite some time, I didn't think I needed to do that) Thanks for any help

The problem is the link still displays the old way in the website

Your rule simply allows new url format and internally rewrites it to its old format equivalent.

If you want old format to redirect to new format (without an infinite loop), you have to add some code.

You can replace your current code by this one

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} \s/featured1new\.php\?homedetails=([^&\s]+)&ID=([^&\s]+)&PHOTOID=([^&\s]+)\s [NC]
RewriteRule ^ /homedetails/%1/ID/%2/PHOTOID/%3? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^homedetails/([^/]+)/ID/([^/]+)/PHOTOID/([^/]+)/?$ /featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3 [L]

NB: even if this code redirects old format , it's better to replace your links with new format

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