2

I have 2 URLs. Basically page and sub page as below.

  • http://example.com/services
  • http://example.com/services/service-1

I want to add a 301 redirect rule in .htaccess file so that when someone opens the page:

http://example.com/services

it should redirect to its sub page:

http://example.com/services/service-1

I tried creating a rule in .htaccess file but it is going into redirection loop as its sub page. Below is my code for .htaccess file:

Redirect 301 /services /services/service-1

Update: Below is entire htaccess file code

RewriteRule ^for-patients$ /for-patients/children [R=302,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
4
Redirect 301 /services /services/service-1

This results in a redirect loop because the mod_alias Redirect directive is prefix matching. So, the source path /services matches the redirected path /services/services-1, etc. etc.

However, since you are already using mod_rewrite (as part of WordPress), you should also perform this redirect using mod_rewrite (as opposed to mod_alias). Different modules execute at different times during the request (mod_rewrite before mod_alias), despite the apparent order in .htaccess, so this can lead to some unexpected conflicts.

So, instead, try the following before your existing WordPress front-controller (ie. before the # BEGIN WordPress comment):

RewriteRule ^services$ /services/service-1 [R=302,L]

This matches the URL /services only. Note there is no slash prefix on the RewriteRule pattern when used in .htaccess.

This is a temporary (302) redirect. Change this to a 301 if this is intended to be permanent, but only after you have confirmed it is working OK. (301s are cached hard by the browser by default, so can make testing problematic.)

UPDATE: To make the above work with (or without) a trailing slash on the end of the requested URL then change the above to:

RewriteRule ^services/?$ /services/service-1 [R=302,L]

And if your target URL should have a trailing slash as well, then append this on the substitution. ie. /services/service-1, otherwise WordPress will either issue another redirect or you potentially create a duplicate content issue.

  • Its not redirecting. Below are the actual URLs. I used different one in the example. RewriteRule ^for-patients$ /for-patients/children [R=302,L] – aslamdoctor Feb 5 '18 at 12:53
  • You will need to make sure your browser cache is cleared and that you have placed this directive at the top of your .htaccess file - the order is important. Please include your entire .htaccess in your question, so we can check this. – MrWhite Feb 5 '18 at 13:39
  • OK I have added entire htaccess file and I am testing it in incognito window in chrome every time I make changes in htaccess file. So if I change one line, I close the existing incognito window and open new one and check again. – aslamdoctor Feb 5 '18 at 13:42
  • Your .htaccess looks OK. Can you just confirm the URL is example.com/for-patients (no trailing slash)? And literally nothing happens, example.com/for-patients remains in the browsers address bar? – MrWhite Feb 5 '18 at 14:50
  • OK here is a thing I noticed if I use it without trailing slash in the end, it is redirecting but as we know wordpress adds trailing slashes in all links so the links with trailing slashes are not redirecting. – aslamdoctor Feb 5 '18 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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