Mobile app version of vmapp.org
Login or Join
Welton855

: Wild card redirect in htaccess giving error this webpage has a redirect loop In my website I changed the directory name "vehicles-cars" to "vehicles-cars-for-sale". When I tried to redirect using

@Welton855

Posted in: #Htaccess #ModRewrite #UrlRewriting

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/" [R=301,L]

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Welton855

1 Comments

Sorted by latest first Latest Oldest Best

 

@Annie201

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 group optional. Instead try this:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?adsbuz.com$
RewriteRule ^vehicles-cars/(.*)$ adsbuz.com/vehicles-cars-for-sale/ [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme