apache mod-rewrite removing www from url - apache

I want to remove www. from my site url.
I have added this to .htaccess:
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
It works if I type www.domain.com/x but not if I type www.domain.com/x/y. The rewrite "eats" the x value.
Thanks for your help.

Try using
RewriteRule (.*) http://mydomain.com%{REQUEST_URI}
for your rewrite rule instead.

Related

Redirect stripping the matched string - Htaccess

Using .htaccess, I want to redirect all the pages which contains --nocolor- at the end of their url. I want redirect them to the same url without the match.
Example:
https://www.test.com/products/test--nocolor-
to
https://www.test.com/products/test
What I've tried is
RewriteCond %{REQUEST_URI} (--nocolor-)
and that is working but I cannot get the right url while rewriting.
I've tried
RewriteRule ^products/([^/]*)(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^(.+)(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^.*(?=--nocolor-) /$1 [R=301,L]
but I always end up with $1 empty.
What do I do wrong?
With your shown samples, attempts please try following .htaccess rules. Make sure to place these rules at top of your .htaccess rules.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*)--nocolor-\s [NC]
RewriteRule ^ /%1 [R=301,L]

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]

.htaccess redirect subdomain dynamicly without changing url

I need to have a .htaccess redirect.
If I go to example.bla.org
I want it to redirect to bla.org/example
EXCEPT: It must keep the subdomain url can needs to be dynamic.
Ex: example.bla.org/apple -> bla.org/example/apple
I have tried almost every method like this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^examples.website.org [NC]
RewriteRule ^/(.*)$ /examples/$1 [L]
Please help, thanks!
You're pretty close, try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.website.org [NC]
RewriteRule ^(.*)$ http://website.org/example/$1 [L]
If you want to do a 301 redirect for SEO etc, add R=301 to the rewrite :
RewriteRule ^(.*)$ http://website.org/example/$1 [R=301,L]

apache domain redirect not working as expected

I have the following rewrite rule in apache conf.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
It redirects http://example.org/?p=18 to http://www.example.org//?p=18. It adds // at the end of domain name. How to fix this?
You have an extra /. You have two choices:
You can avoid putting it in the target:
RewriteRule ^(.*)$ http://www.%1$1 [R=301,L]
Or you can avoid capturing it in the match part:
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]

mod_rewrite and double slash issue

I applied the following mod_rewrite rule in Apache2 to redirect from non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
However, there's a double slash issue:
When I go to http://www.example.com it
correctly rewrites the URL to
http://www.example.com/
When I go to
http://www.example.com/somepage, it correctly
rewrites the URL to
http://www.example.com/somepage
If I
go to http://example.com, it
rewrites the URL to
http://www.example.com// (double final slash)
If I go to
http://example.com/somepage, it
correctly rewrites it to
http://www.example.com/somepage
Is my configuration good for SEO?
Fixed with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
because $1 by default contains the index path /
RewriteRule ^\/?(.*)$ http://www.example.com/$1 [R=301,L]
Actually, you will always have double slashes due to
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
combined with the fact that REQUEST_URI (that you are matching on) normally contains a starting slash. What you can try is RewriteRule ^(.*)$ http://example.com$1, and then send a broken HTTP request GET foo HTTP/1.0 and see if Apache deals with it properly.
Putting a slash into your pattern should resolve this issue:
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
That is because the root path is /, and you are appending whatever you get in RewriteRule (the first case works fine because it doesn't match the condition so no rewrite is performed).
You can try something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# for the home page
RewriteRule ^/$ http://www.example.com/ [R=301,L]
# for the rest of pages
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]