apache domain redirect not working as expected - apache

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]

Related

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]

Mod_rewrite on a dynamic rule not working

I have several web sites and using the same mod_rewrite. I like to add two additional conditions to it and based on many of solutions that I have found on here and none seem to be working.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have my sites working with the following solution.
domain.com forwards to www.domain.com
I like to add the following two conditions to my conditions above.
Remove trailing slashes. If I have www.domain.com/ it does remove
that trailing slash, unfortunately, for some reason if there are
multiple slashes at the end it doesn't www.domain.com//
Removing index.html from www.domain.com/index.html
Making this dynamic is what may be through this off. If I use the suggestion on domain itself, I have not problems. It is once I add http://www.%{HTTP_HOST}%{REQUEST_URI} where everything goes wonky.
i use this rewrite for redirecting domain.com to www.domain.com. No problems with //
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R,L]
#this could work for you (untested)
RewriteCond %{HTTP_HOST} !^www.\$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R,L]
#redirect /index.html to /
RewriteRule ^/index.html / [R=301,L]

.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

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]

apache mod-rewrite removing www from url

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.