0

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

Mdg
  • 45
  • 8
  • Read: http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained and "What mod_rewrite does not do" – Croises Jan 24 '15 at 22:18
  • 1
    Thank you Croises, this link and Justin lurman was a great help! – Mdg Jan 25 '15 at 19:05

1 Answers1

0

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

Justin Iurman
  • 18,312
  • 3
  • 30
  • 48
  • Thank you so much! That worked and explains things much better! I will go thru and replace my old links with the new format also. The only thing now, it changed every link on new page to include the new RewriteRule + the link. – Mdg Jan 24 '15 at 23:44
  • I'm not sure i understood your last sentence – Justin Iurman Jan 25 '15 at 00:33
  • When I clicked on a link, Like "Contact us", the RewriteRule was applied to that link with contact.php appended to it. So, I changed the "new url format" to dashes instead of slashes, then I changed the links in my html to match the new rule, which works perfect! I still cant figure out how to make the "old format" RewriteRule use dashes instead of slashes, however I don't really need that now. Thanks a ton! :-) – Mdg Jan 25 '15 at 02:36
  • Could you give me an example of url with dashes and its slashes equivalent ? – Justin Iurman Jan 25 '15 at 11:54
  • It's now http://test.mysite.com/homedetails-12-Magnolia-Court-Branson-MO-65616-11315943-20140401022411163178000000 Instead of http://test.mysite.com/homedetails/12-Magnolia-Court-Branson-MO-65616/ID/11315943/PHOTOID/20140401022411163178000000 – Mdg Jan 25 '15 at 18:58
  • I don't understand why you replaced slashes by dashes. What was the problem with your `contact` link ? The code i gave you only applies on `featured1new.php` – Justin Iurman Jan 26 '15 at 16:37
  • Sorry Justin, I don't know how to explain it better. The rewrite portion: /homedetails/12-Magnolia-Court-Branson-MO-65616/ID/11315943/PHOTOID/20140401022411163178000000 was showing up in the next pages links breaking them. It looked like: http://test.mysite.com/homedetails/12-Magnolia-Court-Branson-MO-65616/ID/11315943/PHOTOID/20140401022411163178000000/contact.html (notice contact.html added to the link. homedetails/12-Magnolia-Court-Branson-MO-65616/ID/11315943/PHOTOID/20140401022411163178000000 isn't supposed to be there. Changing to Dashes fixed the problem. – Mdg Jan 26 '15 at 17:00
  • Hmmm ok now i got it. That's because you're using **relative** paths instead of **absolute** paths for all your resources (images, css, js, href links, etc). For example, your contact link should be `/contact.html` instead of `contact.html` otherwise you'll have some issue with links containing slashes. Just use absolute paths and my code will be fine for you – Justin Iurman Jan 26 '15 at 17:26
  • Ok, I'll have to go thru and change everything to absolute paths. Tad bit of work, then I'll switch back to the slashes. Thanks! – Mdg Jan 26 '15 at 19:34
  • You can also add `` after `` html tag in all your concerned pages instead of changing each links. Don't forget to validate my answer if it was helpful, in order to mark your question as solved – Justin Iurman Jan 26 '15 at 22:12
  • That works Perfect! Based on your help, I made all my links seo friendly and now using the slash like all the big websites are for displaying properties! – Mdg Jan 26 '15 at 23:09