2

I am trying to rewrite a URL like:

http://example.com/shop/item.php?id=2

To something like:

http://example.com/shop/my-page-title-2.html

Because of the possibility of having multiple items with the same page title, I want to add the ID to the URL, as each ID is unique and will ensure I do not have any duplicate URL's.

The page title should also be rewritten so that everything is in lowercase for the URL.

Any help would be greatly appreciated!

MultiDev
  • 10,389
  • 24
  • 81
  • 148
  • possible duplicate of [.htaccess rewrite GET variables](http://stackoverflow.com/questions/7677070/htaccess-rewrite-get-variables) – Parsa Jul 14 '15 at 20:37
  • @user2662639 While this question has most likely been asked before, that isn't a very good duplicate. –  Jul 14 '15 at 20:41
  • I don't understand why you would want to do this, usually it is rewritten the opposite way. –  Jul 14 '15 at 20:43
  • @TinyGiant I want to rewrite the URL like this for SEO purposes, to have keywords in the URL. – MultiDev Jul 14 '15 at 20:44
  • 1
    I'm still unclear as to your intentions. `mod_rewrite` changes the followed url, it does not change the url visible to the browser, so this would not change what is seen for SEO –  Jul 14 '15 at 20:47

1 Answers1

1

You can the following rule :

RewriteEngine On
RewriteCond %{THE_REQUEST} /shop/item.php\?id=([^&\s]+) [NC] 
RewriteRule ^ /my-page-title-%1\.html? [NC,R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^my-page-title-([^.]+)\.html$ /shop/item.php?id=$1 [QSA,L,NC]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115