Forum Moderators: phranque

Message Too Old, No Replies

URL’s re-write

         

abhishekmishra

7:32 am on Jun 7, 2011 (gmt 0)



Hi There,
One of my client wants to re-write the URL's structure of the website i'm putting one example of same: - eg. www.example.com/abc-89.html Into www.example.com/abc

They want change structure of URL's And remove URL's extensions(.html or .php) as well...

Is it fine..
Kindly brief me on this..

Thanks

Tom_Cash

8:00 am on Jun 7, 2011 (gmt 0)

10+ Year Member



The way you do this is in the .htaccess file. The way I would remove my PHP extensions would be like this...

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

Hope that helps, somewhat.

g1smd

8:35 am on Jun 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There are many previous threads with this information.

You must first change the link on your page to point to the correct URLs.

Next install a rewrite. While the example posted above would work it is very inefficient because every request hitting your server invokes two very slow disk reads. Remove the two RewriteCond lines and change the ^(.*)$ pattern to ^([^/.]+)$ or similar so it simply tests for "extensionless" requests.

A fatal flaw of the previous code is that a request for the URL
example.com/
is mapped to the file
/.php


The final part of the puzzle is to redirect client .html and .php URL requests to the extensionless URL.

However, for the example in the original post this solution cannot be directly used here because there is no way for mod_rewrite to know to add or remove the "-89" part. You are better off retaining the ID number in the new URLs, otherwise you will have to have a database to look up the URL mappings.

abhishekmishra

8:53 am on Jun 7, 2011 (gmt 0)



Thanks Guys,

But; is it effect on current ranking & page rank?

rocknbil

4:16 pm on Jun 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You won't lose indexing with a proper 301 . . . .

rewriteRule ^abc-89.html$ /abc [R=301,L]

... but you'd have to do that for every URL, impractical on a large site.

@g1smd: awesome display of restraint. :-)

Tom_Cash

7:52 am on Jun 8, 2011 (gmt 0)

10+ Year Member



I tried my best. ;) I'm really trying to sharpen my htaccess skills, so forvive my poor code. This has worked for me on several occasions, I thought it would be okay.

What would you recommend g1smd?

RewriteEngine On
RewriteRule ^([^/.]+)$ $1.php [L,QSA]

abhishekmishra

12:23 pm on Jun 8, 2011 (gmt 0)



Thanks..

g1smd

11:48 pm on Jun 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd rewrite to a PHP script and have all the logic in that PHP script (with data in an array for a small site, or in a database for a large site). The PHP script would send the correct headers back.

The array method was discussed about a month ago, with code included.

Tom_Cash

7:42 am on Jun 9, 2011 (gmt 0)

10+ Year Member



Cool, cheers!

bwnbwn

8:46 pm on Jun 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



But; is it effect on current ranking & page rank
The answer to both of these is yes. Changing the url structure will effect both your ranking and PR. Both will probably come back after Google sorts it all out. A time is a complete guess.