Imdad
Imdad

Reputation: 6032

htaccess redirect old URL to new URL

On my website for SEO purpose the link to hotel.php?hotel_id=104 is changed to parkgrand.html and changed htaccess to refer to the same script

code : RewriteRule ^parkgrand\.html$ /hotel.php?hotel_id=104

Till this it works fine. But I want if some user still accesses the old url i.e. hotel.php?hotel_id=104 then he should get automatically redirected to parkgrand.html and user agent should get a response 301

So, the code now becomes

RewriteCond  %{REQUEST_URI} ^/hotel.php
RewriteCond  %{QUERY_STRING} ^hotel_id=104
RewriteRule ^hotel.php /parkgrand.html [L,R=301]

RewriteRule ^parkgrand\.html$ /hotel.php?hotel_id=104

But this is causing a redirect loop and the intended page doesn't gets displayed to the user. Can anyone tell me what should be the correct code for this.

Upvotes: 0

Views: 1781

Answers (1)

Prix
Prix

Reputation: 19528

Give this a try:

# Redirect /hotel.php?hotel_id=104 to /parkgrand.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+hotel\.php\?hotel_id=104[&\s] [NC]
RewriteRule ^ /parkgrand.html? [R=302,L]

Change from R=302 to R=301 once you confirm it is working to avoid caching.

Keep in mind you may have been cached from your previous attempts since you're using R=301, I will recommend you to test the above using a different browser to make sure its working in case your usual browser is cached.

Upvotes: 3

Related Questions