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
Related
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]
I am redirecting all non-www to www.
It works fine for all links unless there is a link that contains a folder in our root directory, like /us/ or /uk/.
This is my rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If I use tools like this one: http://htaccess.madewithlove.be/ it shows that the redirect is working fine:
http://example.com/uk/ redirects to http://www.example.com/uk/
But in reality it redirects to http://www.example.com/. I deleted every cache and varnish and also used tools like this: http://www.redirect-checker.org/index.php and cURL.
The folder path is being dropped. http://example.com/uk/whatever becomes http://www.example.com/whatever
What else could it be?
I inserted my redirects also in a .htaccess file inside the /uk/ folder.
Thanks!
I inserted my redirects also in a .htaccess file inside the /uk/
folder.
You can not do that. It just gives the wrong result you get.
Use yours or this htaccess at root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
You can try to use this rewrite rule instead:
RewriteCond %{HTTP_HOST} ^([a-z.]+)?yoursite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%yoursite.com%{REQUEST_URI} [R=301,L]
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
I'm trying to do an htaccess redirection based on this condition :
Redirect all pages without "www" http://siteweb.com to http://www.siteweb.com/index.html
Except http://siteweb.com/index.php
The http://siteweb.com/index.php must to be redirected to http://www.siteweb.com/index.php
Actually i use this code [but something wrong on it :s]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule !^/index.php$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Thanks in advance.
You can use this code in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (?!^index\.php$)^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NC]
This will redirect all URIs except index.php to a domain with www. However your question is confusing because you also wrote that:
The http://siteweb.com/index.php must to be redirected To http://www.siteweb.com/index.php
Which tells me that may be you want to redirect every URI including index.php to a domain with www.
I have a URL
http://test.devsite-1.com/test/tbox/
which I want to redirect to
http://tbox.devsite-1.com/
Rule:
RewriteCond %{HTTP_HOST} !^tbox\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.|)(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/tbox(/.*|)$
RewriteRule /tbox/(.*) http://tbox.%{HTTP_HOST}/$1 [R=301,L]
I don't understand why it is not redirecting me to the URL? Please note I need a generalized rule so that if I change test.devsite-1.com to tempo.devsite-1.com the same should work with the other URL as well.
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.(.+)$ [NC]
RewriteRule ^test/tbox/(.*)$ http://tbox.%1/$1 [R=301,L]
This will redirect (301 Permanent Redirect)
http://test.devsite-1.com/test/tbox/something-optional
to
http://tbox.devsite-1.com/something-optional
It has to be placed in .htaccess file in website root folder (e.g. http://test.devsite-1.com/.htaccess). If placed elsewhere some tweaking may be required.
It will only work if request is coming via test. subdomain.
And it will only work if URL requested starts with test/tbox/.
All of the above matches your URL examples.