4

on my Wordpress site, I need to

http://example.com/page1/

redirect to

http://site.example.com/page1/

I have tried this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^page1$ "http\:\/\/site\.example\.com\/page1\/" [R=301,L]
</IfModule>

The above did not work. I also removed the double quotes and escape slashes and just directly put the url, but no success. I will really appreciate the help.

1

http://example.com/page1/

RewriteRule ^page1$ ....

You are missing a trailing slash on the end of the RewriteRule pattern. Try the following in the .htaccess file at example.com's document root. This will need to go before any existing WordPress rewrites.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^page1/$ http://site.example.com/page1/ [R=301,L]

I also removed the double quotes and escape slashes ...

Yes, no need for the plethora of backslash escapes. That looks like one of cPanel's attempts.

Also, no need for the <IfModule> wrapper, unless this is expected to "work" without mod_rewrite installed?

The RewriteBase directive is also superfluous here. (Note that you should only have one RewriteBase directive in your .htaccess file - WordPress usually writes this already.)

  • This worked. Thank you so much! just one question, why this need to be put in before any WordPress rewrites. When I put it at the end of htaccess it does not work. I have just put it before # BEGIN WordPress tag and it is working fine. – atul Sep 29 '16 at 18:00
  • You're welcome. If you were to put those directives at the end of .htaccess, after the WordPress rewrites, then it wouldn't work because it's not being run at all. WP rewrites/routes all requests so any mod_rewrite directives that follow are simply ignored. – MrWhite Sep 29 '16 at 18:08
  • in continuation of my above comments - Redirect 301 /hello-world/ http://example.com/ works fine at the end of htaccess. Why such behavior? Please help me understand. Thank you so much in advance! – atul Sep 29 '16 at 18:09
  • Different modules run at different times during the request, but directives within each module execute in order, top-down. Redirect is part of mod_alias, whereas WP (and the above) uses mod_rewrite. These modules execute independently (mod_rewrite usually first). So, the Redirect directive executes regardless of the mod_rewrite directives that precede it. But also, you should never mix external redirects from both modules, because you can end up with unexpected conflicts. – MrWhite Sep 29 '16 at 18:18
0

Also you can try this solution by replacing rewrite rule with below line in the .htaccess.

Redirect /page1/   http://site.example.com/page1/
  • This is creating infinite loop. – atul Sep 29 '16 at 17:34
  • It would seem that example.com and site.example.com share the same filesystem, so you will need to use mod_rewrite and detect the host, by which the site has been accessed, before redirecting - you can't do this with a mod_alias Redirect. – MrWhite Sep 29 '16 at 18:16

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.