3

In my website I changed the directory name "vehicles-cars" to "vehicles-cars-for-sale". When I tried to redirect using a wild card redirect from my old directory name to new directory name in my web hosting cPanel account, I get an error every time I open pages from that directory:

this webpage has a redirect loop

The website is php.

The problem is that that I have lots of pages with the old directory indexed in Google and they are getting duplicate content.

I really need some advice on what to do with this problem.

Here is .htaccess file code for redirect, thanks.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^adsbuz\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.adsbuz\.com$

RewriteRule ^vehicles\-cars\/?(.*)$ "http\:\/\/adsbuz\.com\/vehicles\-cars\-for\-sale\/$1" [R=301,L]
3
  • php has nothing to do with this question, it should only be tagged .htaccess, and maybe apache or regex but not php
    – WebChemist
    Commented Sep 18, 2012 at 7:34
  • @WebChemist While you are correct about the content, it's also generally better for you to go ahead and just make such corrections yourself than wait for the asker(or someone else) to do it.
    – Su'
    Commented Sep 18, 2012 at 9:03
  • @Su' My points aren't high enough to alter tags on other users posts
    – WebChemist
    Commented Sep 18, 2012 at 18:45

1 Answer 1

2

first, your redirect is in an infinite loop because of the question mark after -cars/?

cars/?(.*)

the question mark operator means optional, in that there may or may not be a slash after -cars. So lets say your old url is /vehicles-cars/23. With the rule as you have it, /vehicles-cars/23 matches, and redirects to vehicles-cars-for-sale/23 and the page reloads. But then /vehicles-cars-for-sale/23 matches again since the / can be optional, its matching "-for-sale/23" as (.*), so you wind up with /vehicles-cars-for-sale/-for-sale/23, and then reloads and matches again for /vehicles-cars-for-sale/-for-sale/-for-sale/23 and so on to infinity, because it will always keep matching. Change /? to / and this should stop the infinite loop.

second, you don't need to escape everything like you have here. Dot (.) needs to be escaped as its unescaped meaning is a single wildcard character, but dash (-) and forward slash (/) do not have special meaning, so they do not need to be escaped. And quotes shouldn't be necessary unless there are spaces in the url (a bad idea to begin with). Also you can combine the 2 domain matching conditions into 1 by making the www. group optional. Instead try this:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?adsbuz\.com$    
RewriteRule ^vehicles-cars/(.*)$ http://adsbuz.com/vehicles-cars-for-sale/$1 [R=301,NC,L]

if you need to have the -cars/? conditional for some urls that dont have / after -cars, post additional url examples and I can help you properly forward those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.