Im using the following in my htaccess
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^(.*)$ http://www.website.com [R=301,L]
This works except that i have urls like this website.com/name?query=1
The rewrite is removing the /name when typing the url with non-www
so website.com/name?query=1 will rewrite to www.website.com/?query=1
Also im using Zend Framework and have the default /public folder when using
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^(.*)$ http://www.website.com/$1 [QSA,R=301,L]
in the rewrite it makes the link look like this www.website.com//public/name?query=1
I need to not have it NOT write //public and I don't want to change the structure of the site because there are to many pointers to the /public folder
How do I get the RewriteRule to change non-www to www and not drop "name" ?
Try this.
# No W's to W's keeping all the URI items intact
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^(.*)$ http://www.website.com/$1 [QSA,R=301,L]
Related
I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/
Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/
I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/ will be redirected to http://www.example.com/videos/ URL.
This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The contrary is also possible (www to non-www):
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. Something like this should work site-wide:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
This is Best Solutions to redirect non-www to www URL's using htaccess. using this code in htaccess file and check url.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host.
# Redirect non-www to www only for main domain
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /
# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/$1 [R=301]
Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website i.e. subdomain.yoursite.com or whatever. Than you're welcome to use the below method.
"Saying Again make sure if you really want to use this method"
# Redirect all non-www to www including subdomain(s)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500.
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I have moved a website from www.example.com to www.example.com/subfolder and using an .htaccess file, I would like to redirect old links going to www.example.com to www.example.com/subfolder. My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]
I've also tried the following:
#RewriteCond %{REQUEST_URI} !^/subfolder/
#RewriteRule ^.*$ http://www.example.com/subfolder%{REQUEST_URI} [R=301,QSA,L]
I keep getting redirect loops or the redirect not working and leading to an error 404.
You can use this rule in site root .htaccess (parent directory of subfolder) as the first rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!subfolder/)(.*)$ /subfolder/$1 [L,NC,NE,R=301]
# remaining rules go here
Make sure to clear your browser cache completely when testing this change.
are you using wordpress? Or you just moving in general?
If you are moving in general this one-liner will work
RedirectMatch ^/$ /subfolder/
Original answer from here
This is another solution i'm using currently.
Do not copy and paste. You need to Replace DOMAIN (without the .com) and SUBFOLDER to the name u want.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /subfolder/$1 [L]
I have a domain name domain.com and some folders inside it. When i am accessing the folders inside this domain i can access the folder like this.
http://www.domain.com/folder
I want to convert this url with htaccess to
http://www.folder.domain.com
I would like to have similar urls for all folders so its a wildcard entry.
Please tell me how can it be done with htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://subdomains.domain.com/$1 [L,NC,QSA]
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com [NC]
RewriteRule ^([^/]+)(/.*)?$ http://www.$1.domain.com$2 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
For other you have to set dynamic name and pass as parameter, for simple blog you can take help of this question.
Motive: What I am trying to do is use parked domain and redirect it to subdirectory. and in subdirectory I have a script that needs pretty url's and htaccess is the only thing I can use.
Code in main .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^info/(.+)$ contact.php?a=$1
RewriteRule ^list/(.+)/page/(.+)$ list.php?a=$1&p=$2 [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/m/$1 [R=301]
Code in /m .htaceess
RewriteEngine On
RewriteBase /
RewriteRule ^how/(.+)$ how.php?a=$1
RewriteCond %{HTTP_HOST} ^(www.)?main-example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/m/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
What i want is to achieve when user visits www.example.com/m/how/love the script should treet a get variable of a something like www.example.com/m/how.php?a=love but url should
remain pretty.
What my current code does is unlimited redirects to /m/404.shtml giving 301 in firebug.
What would be the best way to redirect codeigniter website to www domain? The only way that comes to my mind is to use htaccess for this but can't figure out exactly how. This is the rewrite rule I'm using right now to remove index.php from the url. How should I add the redirection to www domain correctly?
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|uploads|robots\.txt|favicon\.png|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
See this URL
.htaccess Redirect non-WWW to WWW preserving URI string
Or try it
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.mysite.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Another example
See this URL
Codeigniter redirects for a new domain
Try it
Try adding the following to the .htaccess file in the root directory of your www.xyz.com site.
RewriteEngine on
RewriteBase /
#redirect www.xyz.com/A/B/controller/function
#www.xyz.com/B/controller/function
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
RewriteRule ^A/(B/[\w]+/[\w]+)$ /$1 [L,NC,R=301]
#redirect www.xyz.com/A/controller/function to
#www.abc.com/controller/function
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
RewriteRule ^A/([\w]+/[\w]+)$ http://www.abc.com/$1 [L,NC,R=301]
Use a CNAME record in your DNS.
This saves an HTTP request so is the most efficient option.
Add another condition under that:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]