Rahul
Rahul

Reputation: 23

Can anybody please help me with some URL rewriting?

I am in the development of a file sharing website now i want to create seo friendly urls.I tried different .htaccess codes to implement that Anybody please help me to implement that

I have a page www.mydomain.com/gallery.php?id=2&title=newimage

I want them to be requested as www.mydomain.com/newimage/1.html

Please give me .htaccess code to rewrite www.mydomain.com/gallery.php?id=2&title=newimage to www.mydomain.com/newimage/1.html

Upvotes: 2

Views: 56

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Assuming that the 1 and 2 are really the same id, you can add these rules to the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReewriteRule ^/?([^/]+)/([^/.]+)\.html$ /gallery.php?id=$2&title=$1 [L]

This makes it so when someone goes to http://mydomain.com/foo/bar.html, they get served the content at /gallery.php?id=bar&title=foo.

In order to correct all the external links beyond your control to start using the new nicer looking URL, you can add these too:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /gallery\.php\?id=([^&]+)&title=([^\ ]+)
RewriteRule ^ /%2/%1.html? [L,R=301]

This makes it so when someone puts http://mydomain.com/gallery.php?id=123456&title=blah, the browser gets redirected to http://mydomain.com/blah/123456.html, thus changing the address in their browser's address bar to the nicer looking URL.

Upvotes: 1

Related Questions