Forum Moderators: phranque

Message Too Old, No Replies

Need help for url rewriting .htaccess

         

sumit_php

7:56 am on Oct 7, 2015 (gmt 0)

10+ Year Member



Hi,

i have url like
domain.com/hotels-details/hotelcode=xxxxx&hotel_key=0&hotel_name=hotel-name&hotel_city=new-delhi
now what i need this should be rewrite like
domain.com/hotels/hotel-name-in-hotel-city-hotelcode-hotelkey.

Thanks

whitespace

11:01 am on Oct 7, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



i need this should be rewrite like
domain.com/hotels/hotel-name-in-hotel-city-hotelcode-hotelkey


Well, it's really the other way round (isn't it?) you want to rewrite from:
- domain.com/hotels/hotel-name-in-hotel-city-hotelcode-hotelkey
to
- domain.com/hotels-details/hotelcode=xxxxx&hotel_key=0&hotel_name=hotel-name&hotel_city=new-delhi

(Assuming you have already changed the URLs in your app to the "domain.com/hotels/hotel-name-in-hotel-city-hotelcode-hotelkey" version?)

And the age old question... What have you tried so far?

sumit_php

11:14 am on Oct 7, 2015 (gmt 0)

10+ Year Member



RewriteCond %{THE_REQUEST} ^GET\ /hotels-details\?hotelcode=(\S+)&hotel_key=(\S+)&hnm=(\S+)&hct=(\S+) [NC]
RewriteRule ^ /hotel/%3-in-%4-%1-%2? [R=301,L,B]

return URL as per my need but showing error 404(page not found)

lucy24

8:45 pm on Oct 7, 2015 (gmt 0)

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



#1 If the rule is only meant to apply to requests for /hotels-details then put that in the body of the rule. Otherwise your server has to evalute conditions on every single request ever.

#2 Whenever a rule creates an external redirect (R flag) include the full protocol-plus-domain in the target.

So the rule quoted above should say
RewriteCond %{THE_REQUEST} ^GET\ /hotels-details\?hotelcode=(\S+)&hotel_key=(\S+)&hnm=(\S+)&hct=(\S+) [NC]
RewriteRule ^hotels-details$ http://example.com/hotel/%3-in-%4-%1-%2? [R=301,L,B]

return URL as per my need but showing error 404(page not found)

Can you rephrase this, please? It isn't clear whether the two elements (the final URL and the 404 error) are what does happen, or what should happen. I assume you are not a native speaker of English. So it may take a few attempts for all of us to understand each other.

If you have made a rule creating a redirect to a new pretty URL, then there must also be a rule creating an internal rewrite to wherever the content really lives. You haven't said anything about this second rule.

If the old messy URL is where the page content still really lives, then you do need a condition involving THE_REQUEST. If the page content lives at some entirely different location, you can drop THE_REQUEST and instead make a condition looking only at QUERY_STRING.

<tangent>
Why is the [B ] flag needed? Just curious. I first thought it might be because some URL elements involve non-Roman scripts, but the Apache docs say [B ] applies only to non-alphanumeric characters. (They may be wrong about this, though. Maybe they mean non-ASCII.)
</tangent>

(I had to write it [B ] with spurious space, because the Forums software made a fuss.)