Ibrahim Azhar Armar Ibrahim Azhar Armar - 1 year ago 76
Apache Configuration Question

htaccess does not redirect primary URI

I have two WordPress installation

example -> contains installation for example.com
|_ blog -> contains installation for example.com/blog

is a directory inside
which already has a WordPress installation. this works fine. I now wanted to redirect users visiting without www (example : http://example.com/blog/some-topic to http://www.example.com/blog/some-topic) hence i added following wild card rule .htaccess redirection rule inside of blog directory, find below content of .htaccess of blog directory

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

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# END WordPress

# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all

# END Wordfence WAF

It redirects correctly for all URI which contains topic like

  • example.com/blog/some-topic is redirected to www.example.com/blog/some-topic

  • example.com/blog/new-topic is redirected to www.example.com/blog/new-topic

And so on... my issue is when i try redirecting just the example.com/blog it redirects me back to www.example.com, what am i missing here ?


Answer Source

Replace your last rule with this

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/blog/$1 [L,R,NE]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download