.htaccess: Combine CakePHP rules with HTTPS redirect - .htaccess

My current .htaccess for working with CakePHP 3 is:
./
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
./webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
My host provider recommends to set up the following .htaccess for automatic https redirects:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://ihredomain.de/$1 [R=301,L]
How am I supposed to combine all these into one working htaccess without breaking Cake's routing?
I am somewhat afraid of the trial-and-error way, because it is a production system.

In the root folder i changed the .htaccess file to
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
That worked for me while all other solutions didn't.

You can use this in your /.htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://ihredomain.de/$1 [R=301,L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>

Related

when i redirect forcefully http to https it shows redirected you too many times

I apply like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Product/Category/([^/]*)$ /Product/index.php?category=$1 [L]
RewriteRule ^Pages/([^/]*)$ /Allpages/?Pages=$1 [L]
RewriteRule ^Products/([^/]*)$ /Product/ProductPage.php?product=$1 [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
but, It shows www.example.com redirected you too many times.
Okay #ravi i have got your problem so please use this solution.
New Code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Product/Category/([^/]*)$ /Product/index.php?category=$1 [L]
RewriteRule ^Pages/([^/]*)$ /Allpages/?Pages=$1 [L]
RewriteRule ^Products/([^/]*)$ /Product/ProductPage.php?product=$1 [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
</IfModule>

htaccess mod_rewrite URL problems

I would like to re-write this url here;
site.com/product.php?s=electricity-at-work-regulations
to
site.com/courses/electricity-at-work-regulations
so change product.php to courses and generally tidy the url up.
Is this possible?
.htacess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^peoplefactor.co.uk$ [NC]
RewriteRule ^(.*)$ http://peoplefactor.co.uk/$1 [L,R=301]
</IfModule>
# Clean URLS
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^blog/([a-zA-Z0-9-/]+)/preview$ /blog/post.php?s=$1&preview=all [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
RewriteRule ^course/([a-zA-Z0-9-/]+)$ /course.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
# Gets rid of www.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
Yes but it would be better the other way around.
You should always call site.com/courses/electricity-at-work-regulations within your application and reroute it to site.com/product.php?s=electricity-at-work-regulations. Something like
RewriteEngine On
RewriteBase /
RewriteRule ^(courses)/([a-zA-Z\-]+)$ product.php?s=$2 [L]

Issue While redirecting from non www URL to www URL

Hi On our website we are facing a issue like when we enter url without www for eg. example.com/xyz in this case after redirect it is converting to www.example.com/?url=xyz but it should be redirect to simply like: www.example.com/xyz
So we are getting extra ?url= in URL
We have used code for redirect non www url to www in our htaccess file. Code for redirect in htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
We have also tried this code but not work:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
Try placing your redirect rule before the routing rules:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

How to make other subdirectory work with cakephp in root?

I have this path in my hosting server with domain http://www.example.com/ and it's running cakephp in the directory home. but now I want my another_project_name appear in browser with http://www.example.com/another_project_name/.
This is my structure :
/www
.htaccess
home/
.htaccess
webroot/
.htaccess
lib/
another_project_name/
This is my /www/.htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ home/webroot/ [L]
RewriteRule (.*) home/webroot/$1 [L]
</IfModule>
How to configure .htaccess file in /www/.htaccess to make my another_project_name work ?
Try this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (another_project_name/.*) $1 [L] # adjust the regex to what you want.
RewriteRule (another_project_name.*) $1 [L] # adjust the regex to what you want.
RewriteRule ^$ home/webroot/ [L]
RewriteRule (.*) home/webroot/$1 [L]
</IfModule>
.htaccess file should be like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?(another_project_name)/(.*)$
RewriteRule ^.*$ - [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
For multiple directories
RewriteCond %{REQUEST_URI} ^/?(another_project_name1 | another_project_name2 | another_project_name3 )/(.*)$

subdomain rewrite rule lithium framework

I plan to have this as a URL:
https://SubDomain.domain.com/<locale>/Controller/action/param1/param2/?query=param3
in Lithium Framework (li3.me)
What is the purpose of the htaccess files in Lithium?
It should redirect me through .htaccess as:
https://domain.com/<locale>/Conttroller/action/param1/param2/?query=param3&subdomain=SubDomain
In Lithium framework there are 3 .htaccess files:
1. /.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
2. /app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
3. /app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Uncomment the line below, to enable HTTP authentication running PHP as a CGI.
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !favicon.ico$
RewriteRule ^ index.php [QSA,L]
</IfModule>
I tried to edit the root /.htaccess file to get the subdomain name as a parameter without any success.
Pass subdomain as parameter
Dynamic subdomain rewrite htaccess
.htaccess Wildcard Subdomains
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !mydomain\.com$ [NC]
RewriteRule ^(.*)$ app/webroot/$1?subdomain=%1 [L]
Do I have to rewrite rules in all 3 .htaccess files? If so, what are the changes needed?
Thanks!

Resources