Trailing slash in URL - not behind parameters - apache

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

Related

Convert apache rewrite rules to lighttpd rules

How to convert this Apache rewrite rules to lighttpd rules?
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
i think this one will work .
url.rewrite-once = (
".*\?(.*)$" => "/index.php?$1",
".*\.(?i)(js|ico|gif|jpg|png|swf|css|html)$" => "$0",
"" => "/index.php")

Redirect to index.php and remove trailing slash .htaccess

There appears to be many questions like this here but can't find an answer that fits my current code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [QSA,L]
This redirects everything to public/index.php which works fine but I want to get rid of the trailing slash so that example.com/foo/ becomes example.com/foo
You can insert a trailing slash removal code before your current rule:
RewriteEngine On
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
## forward all requests to public/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]

cakephp 3.x and htaccess mod_rewrite make my sessions disappear

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' => '/'
]
],

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]

htacces rewrite url - remove folder name from url

I'm tryng to make an htaccess url rewrite for this cases:
www.website.com/index.php/admin/something => www.website.com/admin/something
www.website.com/index.php/website/something => www.website.com/something
www.website.com/index.php/login/something => www.website.com/login/something
note: "something" can be "something1/something2/something3" or "something1/something2" or "something"
I can cut "index.php" with:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
but I can't found a way to remove only "website/" and not "admin/" and "login/" leaving the rest of url.
If taking this approach you will probably have to specify each possible input reference that should not be mapped to your 'website' controller and remap it to index.php/$1 and treat every other request like it is a method of the website controller, mapping it to index.php/website/$1
Something like the below will map anything starting with admin or login to index.php and everything else to the website controller:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^admin.*
RewriteCond %{REQUEST_URI} ^login.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}
RewriteRule ^(.*)$ /index.php?/website/$1 [L]