Forum Moderators: phranque

Message Too Old, No Replies

Rewrite URLs in htaccess not code

         

johnblack

6:59 am on Jun 10, 2011 (gmt 0)



Hi this could be a very stupid question, but I've gotta ask.

I'm re-coding a site so that instead of urls like

www.example.com/item.php?id=1

I can use

www.example.com/item/widget1

where in my db the record for widget1 has an id of 1

I'm Ok with the htaccess to rewrite the second url to required url

e.g. www.example.com/item/widget1 -> www.example.com/item.php?name=widget1

Now originally I was going to hardcode redirects of

www.example.com/item.php?id=1 ->
www.example.com/item.php?name=widget1

so that if there 20 items I'd have to have 20 redirects. But can I not do this in my php code using the header function?

I guess there are various overheads involved which I'm not aware of? Also I guess it would be better to keep all rewrites and redirects in the htaccess file for neatness.

What are the pros and cons of the two redirect methods - htaccess vs php code

Cheers

[Edited:Typo]

g1smd

7:18 am on Jun 10, 2011 (gmt 0)

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



Using .htaccess will be faster but can only be used if you can "assemble" the new URL from the old URL. If extra "parts" need to be included, then you will have to do it in PHP, and the PHP script will look up the new "parts" from the database or from an array.

By the way to be absolutely clear, the rewrite maps an external URL request to an internal filepath, and a redirect maps requests for an old URL to a new URL. That is, the target of a rewrite is a filepath and that does not include a domain name.

johnblack

7:36 am on Jun 10, 2011 (gmt 0)



Hi g1msd

This is what I have so far in the .htaccess

RewriteEngine On
RewriteRule ^item/(.*)/$ item.php?name=$1 [NC,L]

which enables my code to query the database using new urls

But there is no way I can "assemble" the new URL from an old one, so initially I thought I'd have to add entries in htaccess thus

Redirect permanent /item.php?id=1 http://www.example.com/item.php?name=fish
Redirect permanent /item.php?id=2 http://www.example.com/item.php?name=lion
...
Redirect permanent /item.php?id=20 http://www.example.com/item.php?name=gnu

Are you saying rather than hardwire the Redirect in my htaccess for the 20 records (for example), it would be better to let the php code handle the redirection?

I hope that makes sense, I always get confused between rewrites and redirects - hence the edit in the OP.

g1smd

8:02 am on Jun 10, 2011 (gmt 0)

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



Don't use (.*) in the RegEx pattern. Find a more efficient and less greedy pattern.

I do not recommend using a trailing slash for page URLs. A trailing slash denotes a folder or the index page in a folder.

The [NC] flag is redundant. Delete it.


Yes, use some sort of PHP function for the redirect to the new URL. I posted some code about a month ago where an array was used to hold the data.

Never mix Redirect in the same file as RewriteRule. Use RewriteRule for all of the rules.

johnblack

8:09 am on Jun 10, 2011 (gmt 0)



Thanks for the tips g1msd, the rewrite rule I'd copied from somewhere, i.e. not researched it myself

I'll knock up some code to handle the redirects, will probably be less than 20 lines of php ;-)

Thanks for your help on this