Redirecting using 301 rule in .htaccess - .htaccess

I am having a problem with redirecting a page from example.com (to) www.example.com
The code I have is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.subdomain.domain.com/$1 [L,R=301]
And it is not working, any help?

All you need to do is force the www version of your domain? Just do this...
# Force www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

This is what my rewrite looks like, and it works:
## REWRITE RULES
# enable rewrite
RewriteEngine On
RewriteBase /
# enforce a specific domain in the url
RewriteCond %{HTTP_HOST} !^www\.sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.sub.domain.com/$1 [R=301,NC,L]
This rule will redirect anything that is NOT www.sub.domain.com to www.sub.domain.com.

Update: based on your comment It does not redirect and it gives me the openDNS page, I believe your problem is not (yet?) with mod_rewrite.
Try adding Options +FollowSymlinks before RewriteEngine On, then add RewriteBase / after RewriteEngine On.
I recommend reading sections Fatal Redirection and rewrite logging found here.
You can enable mod_rewrite debugging to see what is going on (if anything!):
RewriteLog "/tmp/rewrite.log"`
RewriteLogLevel 5

Related

My htaccess redirect rule is not working for my subdomain

What I am trying to do is redirect all requests coming from
blog.domain.com to www.domain.com/blog
Here is what I have tried so far based on this answer on SO:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^/(.*) http://www.domain.com/$1 [R=301,L]
</IfModule>
What am I doing wrong? How do I fix this?
Have you started the RewriteEngine? Make sure you have this somewhere before your .htaccess rules:
RewriteEngine on
RewriteBase /
Without these, Apache's Rewrite engine won't recognize these rules, and won't start parsing them as you expect.

(.htaccess) 301 redirect with same url structure

I use 301 redirect from non-WWW to WWW domain! Works fine, but links are different.
Example:
from - ggkj.com/posts/godzilla/43
to - www.ggkj.com/index.php?view=posts&author=godzilla&aid=43
my .htaccess 301 redirect code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*) http://www.example\.com/$1 [L,R=301]
.htaccess code: http://pastebin.com/t3FA59uq
This is currently what i use on my website with success:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} !^www.walterbax.ca [NC]
RewriteRule ^(.*)$ http://www.walterbax.ca/$1 [R=301,L]
EDIT: after looking into your question a little further I can tell you that your issue is improperly placed in the file. it must be hitting another rule that is rewriting it into URL variables.
I would start with a basic htaccess for the www. rule for testing and add to it from there.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

Redirect non-www to www with SEO-friendly query strings

I am trying to redirect my site from non-www to www. My site is at [http://www.alennuskoodit.us]. I try to make it so that all requests without www would be redirected to www. Normal stuff so far.
However, if I go to http://alennuskoodit.us I end up here: http://www.alennuskoodit.us/index.php?qstr=http://www.alennuskoodit.us
This is the .htaccess:
Options +FollowSymLinks
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
# going to install folder
RewriteCond %{REQUEST_URI} (.*)/install/?$
RewriteRule ^(.*)$ %1/install/index.php [NE,R,L]
# going to Admin folder
RewriteCond %{REQUEST_URI} (.*)/admin/?$
RewriteRule ^(.*)$ %1/Admin/index.php [NE,R,L]
# working with client side
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
</IfModule>
This is what I tried, which doesn't work:
RewriteCond %{HTTP_HOST} ^alennuskoodit.us [NC]
RewriteRule ^(.*)$ http://www.alennuskoodit.us/$1 [R=301,NC,L]
How could I redirect all queries to http://alennuskoodit.us to http://www.alennuskoodit.us so that I would not end up breaking the other rewrites?
Place your new rule before all the other rules i.e
Options +FollowSymLinks
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{HTTP_HOST} ^alennuskoodit.us$ [NC]
RewriteRule ^(.*)$ http://www.alennuskoodit.us/$1 [R=301,NC,L]
#other rules here
and it should prevent the qstr= param
Step1= You Must Use This Code In htaccess File To Preferred www Version:
RewriteCond %{HTTP_HOST} !^(.).YourDomain.com$ [NC] RewriteRule ^(.)$ http://www.YourDomain.com/$1 [R=301,L]
Step2= You Must Setting Your Preferred Domain In Google WebMaster Tools :
Open up your webmaster tools and click “Settings” right below “Configuration”. To the right look for the “Preferred Domain” and select which domain you prefer With www Or Not.

hiding index.php and all the variables from url

I'am using a mod_rewrite to get the virtual subdomain as a variable in my site
and would like to add a rule to hide the index.php and all the variables in the url
but can't get it to work. Any suggestions please?
This is my actual .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com$
RewriteRule ^$ /index.php?member=%1 [P,L]
Your best bet is probably to send in a support ticket and have the domain wildcarded.
.htaccess seems perfect.
Detail Description:
You will need to ask hosting company to add "wild-card" DNS set up to forward all subdomains to Web root directory. For example *.domain.com to www.domain.com. They can do this by adding A entry in DNS setting
HTACCESS Code
<IfModule mod_rewrite.c>
# Redirect to user blog (with any trailing path)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
RewriteRule ^(.*)$ /index.php?member=$1 [R=301,L]
## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
</IfModule>

how to modify .htaccess file to always redirect to www

I would like to modify my .htaccess file so that when someone comes into my site without typing www the site always redirects them to the www version. For example, if my url is www.abc.com and they just type abc.com, I want to redirect them to abc.com.
Here is my current htaccess file:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Normally I know how to do the redirect, but im having issues since it already has those few lines in there.
I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
(edited to have all the code in the same block)
Add something like this immediately after RewriteEngine on:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
There are two methods available
1) use mod_alias apache module
Redirect permanent /something http://yourwebsite.com/something
2) Add the following entry in your .htaccess / http.conf / yourwebsite.conf in the webserver configuarion directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule ^(.*)$ http://www.yourwebsite.com$1 [R=permanent,L]
If you want redirect example.com to www.example.com you can try below code
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Resources