I have a Wordpress MultiSite, let's say:
- example.com
- two.example.com
- tree.example.com
- ...
- xyz.example.com
Currently, I'm using this:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
to redirect the entry MS to the no-www version.
Now I want to exclude some directories (i.e wp-admin) and pages:
- www.%.example.com/wp-admin/whatever -> no effect
- www.%.example.com/prefixwhatever -> no effect
- www.%.example.com/whatever -> %.example.com/samethever
I saw this code in another question, but it needs some fixes to works for me (i.e. not exclude home page and take the HTTP HOST as a variable).
<IfModule mod_rewrite.c>
RewriteEngine on
#Home: Exclude the home Page
RewriteCond %{REQUEST_URI} !^/$ [OR]
#News: exclude anything that starts with /prefix, /prefix-2 etc
RewriteCond %{REQUEST_URI} !^/(prefix|prefix-2|wp-admin) [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
</IfModule>
Any ideas? :D
Thank you!
EDIT The following is NOT working. It is not redirecting nothing. :(
<IfModule mod_rewrite.c>
RewriteEngine On
# If yes-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# If the request doesn't start /prefix
RewriteCond %{REQUEST_URI} !^/prefix
RewriteCond %{REQUEST_URI} !^/prefiix-2
RewriteCond %{REQUEST_URI} !^/wp-admin
# Rewrite it:
RewriteRule ^/(.*) http://example.com/$1 [L,R=301]
</IfModule>
EDIT 2 This code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/host #physical file
RewriteCond %{REQUEST_URI} !^/test #Wordpress page
RewriteCond %{REQUEST_URI} !^/wp-admin #physical dir
RewriteCond %{REQUEST_URI} !^/wp-login.php #physical file
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
Seems to be working fine with physical files/dirs. With Wordpress pages this is what happen:
- www.example.com/test -> example.com/index.php
- www.example.com/testwhatever -> example.com/index.php
:(
EDIT 3
Now it seems to be working fine, just added a new Cond:
<IfModule mod_rewrite.c>
RewriteEngine on
#If whe're using www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
#prefixes to !^ ignore
RewriteCond %{REQUEST_URI} !^/host
RewriteCond %{REQUEST_URI} !^/aprobar
RewriteCond %{REQUEST_URI} !^/wp-admin
RewriteCond %{REQUEST_URI} !^/wp-login.php
#See at the end about index.php
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
When you're using permalinks, Wordpress do something like this to get the content:
- www.example.com/pretty-url -> www.example.com/index.php?pretty-url
So we need to avoid redirecting index.php
The new problem is that we are NOT doing this:
- www.example.com/index.php -> example.com/index.php -> example.com/
I think we can set a new rule at the very end of .htaccess to rewrite this particular case.