0

Through htaccess file I am trying to redirect /services to /services/onetime but its redirecting me to /services/onetime/onetime/onetime/...

What is the problem with the code and is there a way to do it?

This is the code I have used:

redirect 301 /services/ /services/onetime/
Vikram
  • 167
  • 2
  • 10
  • You state you want to redirect `/services` to `/services/onetime`, but the example code you have posted will only redirect `/services/` (with a trailing slash) - can you please confirm? (I assume `/service/onetime/` is just a typo in your code sample?) – MrWhite Dec 03 '16 at 09:58

2 Answers2

2
redirect 301 /services/ /service/onetime/

(Assuming that is just a typo on the target URL?)

The mod_alias Redirect directive is prefix matching, and appends everything after the match onto the end of the target URL. So, this will also match the redirected URL /services/onetime/ and then append everything after the match (ie. onetime/) onto the end of /services/onetime/, etc. etc. until the browsers redirect limit is reached.

You would need to use the (mod_alias) RedirectMatch directive instead, which uses regex. For example:

RedirectMatch 301 ^/services/$ /services/onetime/

This only matches /services/ and redirects to /services/onetime/ (including the trailing slashes).

Make sure you have cleared any intermediary caches before testing since any previous (erroneous) 301 redirects are likely to have been cached by the browser.

However, if you have existing mod_rewrite directives (ie. RewriteRule) then you should consider using mod_rewrite instead for this redirect, in order to avoid potential conflicts. For example:

RewriteRule ^services/$ /services/onetime/ [R=301,L]

This will probably need to go before any other mod_rewrite directives. It can be easier to test with temporary (302) redirects since they won't be cached by the browser, unlike permanent (301) redirects. So, ensure your browser cache is cleared.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • it my typo mistake, I want to redirect /services/ to /services/onetime/. What you gave is working fine for all other redirection but not for this. – Vikram Dec 04 '16 at 07:19
  • What do you mean "is working fine for all other redirection" - what other redirect? The directive above won't do any other redirect? What happens exactly? (I've updated my answer and explicitly included the trailing slashes in the example code.) – MrWhite Dec 04 '16 at 10:43
  • However, I see in your [other question](http://serverfault.com/questions/818622/add-trailing-slash-wit-url-excluding-some-urls) that you are already using mod_rewrite (if this is indeed the same site)? If this is the case, then you will need to use a mod_rewrite solution (as opposed to mod_alias), as I mentioned in my answer (I've added a code sample). – MrWhite Dec 04 '16 at 10:54
  • other means /faq to /contact-us – Vikram Dec 04 '16 at 11:11
  • I am trying with mod_alias but no luck yet – Vikram Dec 04 '16 at 11:11
  • So, how are you doing the " /faq to /contact-us" redirect? – MrWhite Dec 04 '16 at 12:45
  • redirect 301 /dev/ /contactus/. but for services its not working redirect 301 /services/ /services/onetime/ – Vikram Dec 04 '16 at 12:52
  • As I stated in my answer, you need to use `RedirectMatch`, not `Redirect` in this instance. Using `Redirect` like that will result in an infinite redirect-loop (which you are seeing) because it is prefix matching. – MrWhite Dec 04 '16 at 13:01
-1

This should work (unless there are other rules interfering):

RewriteEngine on
RewriteRule ^(.*)services /services/onetime [L,QSA]
  • 1
    This will also experience a similar redirect loop, except this is an _internal_ redirect, as opposed to an _external_ redirect, as stated in the question. This also matches a lot more than just `/services` (such as `/path/to/services`). – MrWhite Dec 03 '16 at 09:41