Htaccess non www to www redirect - apache

i'm using the following code to force non www URLs to redirect to www URLs;
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
However, when I visit mydomain.com it does not redirect to www.mydomain.com.
I'm using Red Hat Linux and wondering if there is anything else I need to add to the htaccess to get this to work?
Thanks.

Change your rewrite rule as follows:
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC,QSA]

Related

htaccess redirect for load a specific image url at subdomain from main domain

I have a subdomain (us.example.com) and main domain (example.com). I want to redirect my subdomain url to main domain only for a specific image. For example if I hit http://us.example.com/images/test.jpg then it should redirect to http://example.com/images/test.jpg
I am trying 301 redirect as below but it's not working.
Redirect 301 https://us.example.com/images/test.jpg https://example.com/images/test.jpg
I have tried other rules also but they all work on complete image directory not for a specific image under directory.
Update
Changed rule like this -
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
But no luck.
The Redirect directive from mod_alias has the following syntax:
Redirect [status] [URL-path] URL
and, as you see, it receives/accepts the URL path, and not the whole URI/URL for matching.
For the subdomain match, use an <if> directive:
<If "%{HTTP_HOST} == 'us.example.com'">
RedirectPermanent /imges/test.jpg https://example.com/images/test.jpg
</If>
The same can be achieved using mod_rewrite as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
Try this mate ,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
</IfModule>
What will happen for the above .htaccess was ,
If you visit
us.domain.com/somelink
It will redirect to
domain.com/somelink
EDIT :
Suppose if the above doesn't help try the one below with symlinks
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^us.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
Update 1 :
For only test.jpg
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com/images/test.jpg [NC]
RewriteRule (.*) http://www.domain.com/images/test.jpg [R=301,L]
</IfModule>

http to https redirect

I try to achieve the following behavior with .htaccess:
Redirect http://www.mydomain.tld -> https://mydomain.tld
Also redirect my old links like:
http://www.mydomain.tld/news/mynews should be redirected to https://mydomain.tld/news/mynews
The page should now only be available over https without the leading "www".
I have this rule to redirect from http://www.mydomain.tld to http://mydomain.tld:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
Force https
<IfModule mod_headers.c>
Header set Strict-Transport-Security max-age=16070400;
</IfModule>
But I have problems to figure out how to correctly write the conditions so that I'm not losing my build up link power (Redirect 301 is important).
Hopefully anyone can help me!
Thank you!
I redirect everything to https://example.com without www.
This is what I use:
#Redirect www and http
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Never had a problem with it like this.

.htaccess addon domain issue gives 500 error

This is my .htaccess located at public_html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I have a (legacy) CakePHP app running in public_html/app for maindomain.com
I added an addon domain otherdomain.com under public_html/otherdomain.
However, when trying to load the index page it returns a 500 server error.
Removing the public_html/.htaccess permits browsing otherdomain.com correctly, but the CakePHP app (maindomain.com) then has routing issues.
This is the CakePHP .htaccess under public_html/app:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
it appears that your main htaccess is redirecting traffic to your app/webroot/ which is causing problems for otherdomain.com.
Maybe you can check the hostname that is entered and redirect based on that.
Something like this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^maindomain\.com [NC]
RewriteRule ^(.*)$ app/webroot/$1 [L]
RewriteCond %{HTTP_HOST} ^otherdomain\.com [NC]
RewriteRule (.*) http://otherdomain.com/$1 [L]
</IfModule>

Bypass/exclude a specific set of URLs from a rewriteRule

I'm trying to put a mobile site live on a subdomain. e.g. m.domain.com there is already a desktop site on domain.com.
The domain.com htaccess file has a rewrite rule to redirect all non-www requests to www.domain.com. This is conflicting with the m. subdomain, causing the user to be taken to www.m.domain.com.
Can I add some kind of exclusion to the rewrite rule? Or perhaps specifically overrule the rewriterule?
My non-www to www rewriteRule is as follows:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
You can use this rule to avoid impacting m.domain.com from www rule:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

Apache Mod_rewrite 301 redirection

I am new to Apache. I am trying to make a permanent 301 redirect to the following URL via apache mod_rewrite:
http://www.mysite.com/products.php?page=TheForm
to
http://www.mysite.com/the-form/
The problem is that we have a query string in the first URL. How can I deal with this situation as normally I used the following code but the query string cannot be passed to the RewriteRule.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
RewriteRule ^products.php?page=TheForm$ http://www.mysite.com/the-form [R=301,L]
</IfModule>
The URI-path tested in the rewrite rule does not contain the query. The QUERY_STRING variable should be used.
You may try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} page=TheForm [NC]
RewriteRule ^products\.php /the-form/? [R=301,NC,L]