HTACCESS Rewrite on directories - apache

I have the following code in my Root htaccess file
RewriteCond %{HTTP_HOST} ^paperviewmagazine.com
RewriteRule (.*) http://www.paperviewmagazine.com/$1 [R=301,L]
It works fine for the main site, but for my forums at /forums/ if someone misses off the www it will show the page and not redirect to the www.paperviewmagazine.com/forums/ instead.
I need to force the WWW to prevent anyone from logging in by accident on the non-www as it wont have the correct cookie credentials for accessing the site at www./forums/
Can anyone help?
Thanks.

If you are just trying to route "site.com" to "www.site.com" then do:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R=301,NE]

Related

RewriteRule showing inconsistence behavior

I have the following rule in my htaccess file (this is the full htaccess file):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [L,R=301]
Using the htaccess tester (http://htaccess.madewithlove.be/) it shows the non-www URL is correctly redirected to a www url (test with mydomain.com/ and mydomain.com/subdirectory/ (goes to www.mydomain.com/subdirectory/).
Now, when I put this htaccess file on my site, it will redirect mydomain.com/subdirectory/ to www.mydomain.com instead of to www.mydomain.com/subdirectory/
Why does it show this inconsistent behavior?
You're not using capture value $1 in target:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
So, I looked up what the HTTP_HOST value actually is. It looks like it doesn't contain any further than the domain extension (i.e. domain.com but not anything that's behind that) so it will redirect to domain.com)
I modified the htaccess to the following, which is working:
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If anyone still has any suggestions how to improve the regex, I'm open for improvements

.htaccess redirect non-www to www except one directory not working

I want to redirect all non-www to www. Except for the requests pointing inside /cgi-bin/mail-dada/
I can't think of why the code below also redirects http://something.com/cgi-bin/mail-dada/mail.cgi/admin/ to the www-prepended version.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.something\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/mail-dada/ [NC]
RewriteRule (.*) http://www.something.com/$1 [R=301,L]
The rule in your question should be working.
I suspect a cache problem (maybe old rules ?).
Try it with another browser or clear your cache and try again.
Also, if you don't want to hard-code your domain name, you can do it that way (if you have only www or non-www subdomains)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/mail-dada/ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]

.htaccess Redirection [force www except index.php]

I'm trying to do an htaccess redirection based on this condition :
Redirect all pages without "www" http://siteweb.com to http://www.siteweb.com/index.html
Except http://siteweb.com/index.php
The http://siteweb.com/index.php must to be redirected to http://www.siteweb.com/index.php
Actually i use this code [but something wrong on it :s]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule !^/index.php$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Thanks in advance.
You can use this code in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (?!^index\.php$)^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NC]
This will redirect all URIs except index.php to a domain with www. However your question is confusing because you also wrote that:
The http://siteweb.com/index.php must to be redirected To http://www.siteweb.com/index.php
Which tells me that may be you want to redirect every URI including index.php to a domain with www.

RewriteCond and RewriteRule in .htaccess

I have a client folder located at http://www.example.com/client
However, I've now installed SSL on the server, and want to add a permanent redirect using HTACCESS so that whenever /client is accessed, that it redirects to: https://www.example.com/client
Anybody know how to do that?
I've redirected my domains in the past like this:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This should not affect the solution, but the site must still redirect to www.example.com FIRST, and then to https://www.example.com/client if for example, http://www.example.co.za/client is entered.
Try this:
RewriteCond %{HTTPS} !on
RewriteRule ^client(/.*)?$ https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteRule ^/?$ https://www.example.com/client [301,NC,L]
It tells the apache, whenever the url is https://www.example.com or either with slash at the end, will redirect to ur /client