modrewrite and redirect to https with www - apache

I am trying to use mod_rewrite and redirect al ltrafic to https version. with www.
I have tried a lot of combinations and I can not find final solution.
Currently I have:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^(www\.)?ppp.cz
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
This works for if user enters url with www. Si I have tried to improve the code to (Where is condition for situation when url is without www.):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^(www\.)?ppp.cz
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^ppp\.cz
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
But it is still not doing what I need it to do.
I am currently hopeless.
(Condition with public/ is in all cases working.)
Could anyone help? Thank you
What I want to achieve is http://ppp.cz -> https://www.ppp.cz ; http://www.ppp.cz -> https://www.ppp.cz

Well I have solved it like this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^ppp\.cz
RewriteRule ^(.*)$ https://www.ppp.cz/$1 [R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^www.ppp.cz
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>

Related

Apache .htaccess, subfolder

I have website on Phalcon and everything works fine (I have SSL) but now I would like to have an access to the forum (https://www.example.com/forum/) but I have a problem with the .htaccess configuration.
Phalcon have the specific config:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
What is the correct configuration for home domain and /forum/ folder?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ "http://www.example.com%{REQUEST_URI}" [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ "https://%{SERVER_NAME}%{REQUEST_URI}" [R,L]
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^.*$ public/$0 [L]
Phalcon's root .htaccess file redirects all traffic to /public folder.
What you need to do is add one more condition to your final rewrite rule. Here is portion of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
# Forward to public/
RewriteRule ^(forum)($|/) - [L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
This RewriteRule ^(forum)($|/) - [L] is the important condition you need to add before RewriteRule ^.*$ public/$0 [L]
Also you can add as much as rules you want to allow multiple folders.
RewriteRule ^(forum)($|/) - [L]
RewriteRule ^(some-other-folder)($|/) - [L]
...

How To exclude a Folder from 301 Redirect .htaccess

I have a 301 redirect on the domain goodereader.com that points to goodereader.com/blog/
I wanted to setup a forum, and wanted to exclude /forum/ from being 301'd
Here my current .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
From the comments, you can fix this by adding the same condition to the 2nd rule. Rewrite conditions only get applied to the immediately following rule, they're not global in any way.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
or use a pass through:
RewriteEngine On
RewriteRule ^forum/ - [L]
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
Make sure to clear your browser's cache. 301 redirects are permanent and the browser will always cache them.

htaccess multiple rules and http to https

I'm trying figure out the rewrite rules. We currently have the following rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(blog\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(public|scripts)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
And we need an additional rule that redirects all HTTP traffic to HTTPS.
I've tried the following, but won't work.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^(blog\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(public|scripts)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
Try adding a L flag to your redirect rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTP_HOST} ^(blog\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(public|scripts)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

Redirect HTTPS to HTTP and NON WWW to WWW

I have a problem with my redirects and im guessing its because i keep copy and pasting my htaccess rules.
I need to redirect all https traffic to http, and all non www to www. I also have some more rules to make pretty links
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule (.*\.php)\.$ /$1 [R=301,L]
RewriteRule ^people/([^/]*)/([^/]*)/([^/]*)$ /profile.php?county=$1&name=$2&id=$3 [L]
RewriteRule ^people-in-(.*) /people.php?county=$1 [L]
RewriteCond %{HTTP_HOST} ^ties\.co.uk$
RewriteRule (.*) http://www.ties.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteRule (.*) / [R=301,L]
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
ErrorDocument 404 /notfound.php
You can combine:
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
and
RewriteCond %{HTTP_HOST} ^ties\.co.uk$
RewriteRule (.*) http://www.ties.co.uk/$1 [R=301,L]
into a single rule that needs to be right below RewriteEngine On:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^ties\.co\.uk$ [NC]
RewriteRUle ^(.*)$ http://www.ties.co.uk/$1 [R=301,L]

redirect all non www to www. for Cakephp site

Hi allm i'm having some issues in redirecting the non www pages to the www. pages for my cakephp site.
I've tried
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
but it doesn't seem to work.
My cakephp app resides in a subfolder. eg. www.domain.com/my.
i've added the above code to the root folder, www.domain.com/
any suggestions? thanks?
*update
This my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L]
i'm getting an extra webroot/index.php?url= inserted into the url. Instead of www.domain.com/my/apple, i'm getting www.domain.com/my/webroot/index.php?url=apple
thanks to all once again.
Your code is not quite right. See below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ http://www.domain.com%{REQUEST_URI} [R=301,L] # <-- Mind the 'L'!
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
if domain is .co.uk
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.domain.co.uk%{REQUEST_URI} [R=301,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app/webroot/$1 [QSA,L]
I have tried the below code below and it worked for me:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^gigsadda\.com$ [NC]
RewriteRule ^.*$ http://www.gigsadda.com%{REQUEST_URI} [R=301,L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>