What's wrong with this .htaccess file? - apache

I have a partially working .htaccess file and can’t for the life of me figure out what’s wrong.
Here’s the goal: I have example.com whose canonical form I want to be www.example.com. I have that working okay. I also have a subdomain located in the folder /lang/chinese which I want to resolve as china.example.net. This works fine too. Lastly I have the (parked) domain example.net, which I want to be redirected to example.com and to resolve therefore as www.example.com.
It’s this last part that doesn’t work. If I put www.example.net in my browser, that stays in the address bar.
Here’s the relevant portion of my .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} china.example.com [NC]
RewriteRule ^(.*)$ http://china.example.com$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/lang/chinese/(.*)$ http://china.example.com/$1
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,nc,L]
Clearly I am doing something wrong here. How can I fix this?

For this rule:
RewriteCond %{http_host} ^example\.net [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,nc,L]
the ^ is telling the regex to match against the beginning of the line. So that means you’re never actually searching for “www” there.
I believe you’ll want to change that to:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net [nc]
Info from Apache:
RewriteRule Directive
RewriteCond Directive

Related

Force HTTPS and WWW in .htaccess

I start reading about a similar topic at this page .htaccess - how to force "www." in a generic way? and the solution was not, well almost what I am looking to do.
The problem : I need the user to be on HTTPS and on WWW to make my application working properly. But if some one click on a html link like:
www.example.com
The user will fall on my website with this :
https://www.www.example.com/
Here is my current .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.%{SERVER_NAME}/$1 [R=301,L]
</IfModule>
Is there any way to detect that the user already entered the WWW or is there a best practice to get the result I am looking for?
Thank you.
You are getting this behavior because http -> https rule is adding www\. in target URL without checking if URL is already starting with www.
You should replace both of your rules with this single rule and as a bonus avoid multiple redirects:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

RewriteRule showing inconsistence behavior

I have the following rule in my htaccess file (this is the full htaccess file):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [L,R=301]
Using the htaccess tester (http://htaccess.madewithlove.be/) it shows the non-www URL is correctly redirected to a www url (test with mydomain.com/ and mydomain.com/subdirectory/ (goes to www.mydomain.com/subdirectory/).
Now, when I put this htaccess file on my site, it will redirect mydomain.com/subdirectory/ to www.mydomain.com instead of to www.mydomain.com/subdirectory/
Why does it show this inconsistent behavior?
You're not using capture value $1 in target:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
So, I looked up what the HTTP_HOST value actually is. It looks like it doesn't contain any further than the domain extension (i.e. domain.com but not anything that's behind that) so it will redirect to domain.com)
I modified the htaccess to the following, which is working:
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If anyone still has any suggestions how to improve the regex, I'm open for improvements

.htaccess change domain but keep path

i don't even know whether this is possible or not.
i've 3 domain names:
mytest.com
test88.com
test99.com
mytest.com is the main domain where all the content is located. in my case it is wordpress which is installed on that webspace.
my htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [R,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [R,L]
i want to keep the domainname in case a visitor goes to test88.com but i also want to keep the rest of the path. It should look like this in the address-bar:
http://www.test.88.com/wp/?page_id=10&test=test88
ist this possible?
thanks in advance
Do you want the content from mytest.com show up under the test88.com and test99.com, basically creating duplicates ?
In that case you probably don't want to mod_rewrite to redirect [R], but to reverse-proxy [PT] the content from the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [PT,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [PT,L]
Make sure mod_proxy is installed on your Apache.

.htaccess - redirect domain name

I have a domain name www.domainA.com I want to redirect it to domainB in following manner.
www.domainA.com -> www.domainB.com
www.domainA.com/anything -> www.domainB.com/rebrand
How I can do this in htaccess, I have done following code but it redirecting to /rebrand/ only.
RewriteCond %{REQUEST_URI} ^\/
RewriteRule ^\/$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainB.com/rebrand/ [L,R=301]
URIs that go through rules in an htaccess file has the leading slash stripped off, so you can't match against it. For the second rule, it's matching the / request because the first rule isn't being applied and your regex matches anything or nothing, you can fix that by changing the * to +:
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.+)$ http://www.domainB.com/rebrand/ [L,R=301]
Redirecting through htaccess is tricky sometimes, there are many ways of implementing this but there is one simple way which worked for me
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) [newdomain.com...] [R=301,L]
You can get more info from webmaster world forum