cakephp 3.x and htaccess mod_rewrite make my sessions disappear - apache

When you google for this problem, one can find lots of simular problems but no single solution.
I've got this website:
www.website.com
My CakePhp 3.x project is located in this directory
www.website.com/project
Sessions are used in a previous project so I have to customise the session in app.php in order to access them
'Session' => [
'defaults' => 'php',
'cookie' => 'PHPSESSID',
'timeout' => '20000',
'ini' => [
'session.cookie_domain' => '.website.com',
'session.save_path' => '/var/www/clients/client1/web/tmp/',
]
],
!! All is fine browsing http://www.website.com/project It keeps the session intact!!
In htaccess I make the /project folder invisible so:
http://www.website.com/project becomes http://www.website.com/
http://www.website.com/project/start becomes http://www.website.com/start
...
Now; http://www.website.com/ and http://www.website.com/start are loosing my sessions
This is my htaccess-file:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !website.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^website\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.website\.com$
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteRule (.*) http://www.website.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
#rewrite previous project to root
RewriteRule ^/?$ "http\:\/\/www\.website\.com\/sys" [R=301,L]
#do nothing with these urls
RewriteCond %{REQUEST_URI} !^/portret/.*$
RewriteCond %{REQUEST_URI} !^/temp/.*$
RewriteCond %{REQUEST_URI} !^/tempcs/.*$
RewriteCond %{REQUEST_URI} !^/tempmail/.*$
RewriteCond %{REQUEST_URI} !^/tempsoc/.*$
RewriteCond %{REQUEST_URI} !^/tempzip/.*$
RewriteCond %{REQUEST_URI} !^/web/.*$
#disable project folder to make it nice
RewriteRule (.*) /project/$1 [QSA,L]
#fixing some pains-in-the-ass
Redirect 301 /FDM http://old-website/web/index1.php?ID=FDM
Redirect 301 /GM http://old-website.be/web/index1.php?ID=GM
Redirect 301 /FC http://old-website.be/web/index1.php?ID=FC
#set the root
DirectoryIndex project/search/start
Some debug-info:
session_get_cookie_params
Array ( [lifetime] => 0 [path] => /project/ [domain] => .website.com [secure] => [httponly] => 1 )
CakePhp Debugkit Cookie Request on a page without /project
__unameb00ce0-152452f3c4a-10c277e4-12
PHPSESSID j1guvr5armlfpa7aa64aa9lfe1
_gaGA1.2.1776275125.1452677383
_gat1
CakePhp Debugkit Cookie Request on a page with /project
search Q2FrZQ==.MWQ3MzRhMWY3YWVmMzY0OGEzN2QwODFjMmY0MDE3NTdiOTI4OTQ5NDYxNmNhNmYwNzMyMjRmOTExNDA5NDJlOR+xldRH1lAaIbKGzowwWjtleNpkY/NYKZjft08u3Q8C
PHPSESSID 3amnr19lag1l0s7v5f20us4rq7
language Q2FrZQ==.ZDdjOWQ2NmI2ZDhiNWYzZGM2Zjg4MjRkM2NjMzRjMGRiMmM3NzM4MmEzMzRmZTFkNDI2NDhkY2U4MjBhOGZjM/ARPIqb98q5NwFN9JLi/HelqGCM6V3bsdCFlxxOkI/z
__unameb00ce0-152452f3c4a-10c277e4-12
_gat1
_gaGA1.2.1776275125.1452677383

The following settings worked for me:
'Session' => [
'defaults' => 'php',
'ini' => [
'session.cookie_path' => '/'
]
],

Related

htaccess: redirect to https://subdomain.domain.com

I want to redirect my website to https://subdomain.domain.com in my .htaccess file.
Redirects:
http://subdomain.domain.com => https://subdomain.domain.com
http://www.subdomain.domain.com => https://subdomain.domain.com
https://www.subdomain.domain.com => https://subdomain.domain.com
https://subdomain.domain.com => no redirects
This should do it…
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
First rule redirect all www to https://subdomain.domain.com, second all http to https://subdomain.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://subdomain.domain.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://subdomain.domain.com/$1 [R=301,L]

Trailing slash in URL - not behind parameters

I want a trailing slash in my URLs, but not after URL parameters, if there are any.
Currently using:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
Which works for:
www.example.com/test => www.example.com/test/
www.example.com/test/more => www.example.com/test/more/
but it doesn't work as expected for:
www.example.com/test?param=1&rating=3 => www.example.com/test?param=1&rating=3/
www.example.com/test/?param=1&rating=3 => www.example.com/test/?param=1&rating=3/
The last ones should end up like this:
www.example.com/test?param=1&rating=3 => www.example.com/test/?param=1&rating=3
www.example.com/test/?param=1&rating=3 => www.example.com/test/?param=1&rating=3

Rewrite non-dynamic pages using htaccess

I've been banging my head since many hours on this and I am unable to come up with a solution.
What I need to get done.
domain.com/page.php => domain.com/page/
domain.com/category1-name1.php => domain.com/category1/name1/
domain.com/category2-name1.php => domain.com/category2/name1/
Details: I have couple of files with the following names & I want them to be shown in the following way.
category1-name1.php => category1/name1/
category1-name2.php => category1/name2/
category1-name3.php => category1/name3/
category2-name100.php => category2/name100/
category2-something.php => category2/something/
category2-lipsum.php => category2/lipsum/
Also, I would like to achieve the following rewrite too
/category.php => /category/
/abcd.php => /abcd/
I'm trying but I can't achieve any of it.
I tried this code:
RewriteEngine On
RewriteRule ^category1/([^/]+)/?$ category1-$1.php [L]
RewriteRule ^category2/([^/]+)/?$ category2-$1.php [L]
RewriteRule ^([^/]*)/?$ $1.php [L]
Its only working for /abcd.php to /abcd/ and its throwing 404 errors on rest of the pages, even index is 404 due to it.
Try the following rules in your /htaccess file :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^.-]+)\.php [NC]
RewriteRule ^ %1/ [NC,R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^-]+)-([^.]+)\.php [NC]
RewriteRule ^ %1/%2/ [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1-$2.php [QSA,NC,L]

Yii2 hide a web part in URL using a .htaccess

I have created a .htaccess file in my root folder that contains the following code:
-RewriteEngine On
Add a comment to this line
-RewriteCond %{REQUEST_URI} !^/web
-RewriteRule ^/?([^/]+)$ /web/$1 [L]
but it doesn't work as expected. I also have a .htaccess file in web folder that contains the following code:
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
What am I doing wrong, here?
Put in .htaccess:
RewriteRule . /web/index.php
Or
RewriteRule . /basic/web/index.php
EDIT.
I find better solution. Put in .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
Change domain.com to your domain.
should change the AppAsset public $css path
Add .htaccess outside of the web folder
AddDefaultCharset UTF-8
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Add following in config/web.php
'urlManager' => array(
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
move index.php from inside of the web folder to outside and change these 3 lines
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/config/web.php');
This is working for me in both development and production.

htaccess redirect from www to normal domain except one folder

I need have redirect from www.domain.com to domain.com all calls except one folder "/service/*". For example
http://www.domain.com/ => http://domain.com/
http://www.domain.com/users/* => http://domain.com/users/*
http://www.domain.com/service/* => http://www.domain.com/service/*
I cannot write correct .htaccess rule.
My current htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RedirectMatch 403 /\..*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php