Trailing slash at end of URL bug .htaccess - apache

My problem is, when I enter http://example.com/admin/ it works well, $_GET['decode'] contains right information and I can work with it. But when I enter http://example.com/admin (without slash), my URL redirects to http://example.com/admin/?decode=admin and everything is messed up. Could someone helps me?
Here is my .htaccess:
RewriteEngine on
Options +FollowSymlinks
<FilesMatch "(config.php|defines.php|functions.php)">
Order Allow,Deny
Deny from all
</FilesMatch>
Header set X-UA-Compatible "IE=Edge,chrome=1"
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^\.]+)$ ./index.php?decode=$1 [L,QSA]
php_value date.timezone "Europe/Bratislava"

It is because mod_dir is adding a trailing slash in the directory URI (/admin) after your last rule is run by mod_rewrite.
Try this code:
DirectorySlash Off
RewriteEngine on
Options +FollowSymlinks
<FilesMatch "(config.php|defines.php|functions.php)">
Order Allow,Deny
Deny from all
</FilesMatch>
Header set X-UA-Compatible "IE=Edge,chrome=1"
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,NE,L]
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ ./index.php?decode=$1 [L,QSA]
php_value date.timezone "Europe/Bratislava"

Related

Laravel redirect www to non-www with htaccess not working correctly

I have the following lines inside an .htaccess file which redirects a www URL to a non-www URL:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
It works correctly when trying to load the index route: https://www.example.com to https://example.com, but if someone tries to load any other route, it does not load anything, for example, if I try to load https://www.example.com/about-us it converts the URL to https://example.com/index.php and it doesn't load anything.
How can I fix this?
EDIT
My complete .htaccess is the following:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.pdf|\.mp4|\.woff|\.woff2|\.ttf|\.js|\.png|\.jpg|\.jpeg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images|favicon|fonts|font|videos|storage|pdf)/(.*)$ public/$1/$2 [L,NC]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
<FilesMatch ".(eot|ttf|otf|woff|woff2)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
<Files .env>
order allow,deny
Deny from all
</Files>
you can try this
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

htaccess trailing slashes just for one URL

Having the following .htaccess rules
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^dashboard/(.*)$ back/index.html [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I want to redirect all incomings requests if they match /dashboard to end with a trailing slash. But only for this case.
if I use
RewriteRule ^dashboard/(.*)$ back/index.html [L]
RewriteCond %{REQUEST_URI}dashboard /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
will fail because all my URL's are becoming a trailing slash.
How can I have a trailing slash only for this case?
Have it this way:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^dashboard/(.*)$ back/index.html [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# add / after /dashboard
RewriteRule ^dashboard$ $0/ [L,R=301,NC]
# remove / from all other URIs
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule !^dashboard/ %1 [L,R=301,NC]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Make sure to test it after completely clearing browser cache.

redirecting homepage without affecting other rules

My current .htaccess is given below.
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I want to redirect my homepage to another domain, but maintain all the /sub/directories.
So changed the rules as below.
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# Send request via index.php (if it's not an existing file/folder)
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#redirect
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://example.com/ [L,R=301]
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Now all the /sub/directories are getting internel server error. Logs suggesting it's going on loops.
Any idea?
RewriteCond is only for the next RewriteRule.
Use that:
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
#redirect
RewriteRule ^$ http://example.com/ [L,R=301]
# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Having problems rewriting a folder location with Mod Rewrite

I have the below code in my .htaccess file, this all works fine.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteRule ^(.*),(.*)$ $2.php?rewrite_params=$1&page_url=$2
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteRule ^stores/([0-9a-z.-]+)/?$ shop.php?shop_id=$1 [L,QSA,NC]
RewriteRule ^stores/([0-9a-z.-]+)/ajax_files/watch_item\.php$ ajax_files/watch_item.php [L,QSA,NC]
RewriteRule ^stores/([0-9a-z.-]+)/ajax_files/save_field\.php$ ajax_files/save_field.php [L,QSA,NC]
RewriteRule ^stores/([0-9a-z.-]+)/images/form-cb-icons\.png$ images/form-cb-icons.png [L,NC]
RewriteRule ^group-break/([0-9]+)/[0-9a-z.-]+/?$ group_break.php?cb_id=$1 [L,NC]
RewriteRule ^group-breaks/([a-z]+)-[a-z-]+/?$ group_breaks.php?tab=$1 [L,NC]
However I'm trying to add a new rule at the bottom which is this:
RewriteRule ^billing/client/login/?$ login.php [L,NC]
However this doesn't appear to work. Not sure if it matters or not but the billing directory also has a .htaccess file which may be overwriting it? The contents of that file are below:
<Files ~ "\.(pdt)$">
order deny,allow
deny from all
</Files>
# Protect against Clickjacking
#Header append X-Frame-Options "SAMEORIGIN"
RewriteEngine on
# Force HTTPS
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=307,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
RewriteCond %{REQUEST_URI} ^(.*)/install.php$
RewriteRule install.php %1/install/ [R=301,L]
I wanted to put my code in the root .htaccess file as the one inside the billing dir is for other software which may get overwritten.
Can this be done?
Yes your guess is right. If /billing/.htaccess exists with some rewrite rules then you need to add this rule in /billing/.htaccess file:
<Files ~ "\.(pdt)$">
order deny,allow
deny from all
</Files>
# Protect against Clickjacking
#Header append X-Frame-Options "SAMEORIGIN"
RewriteEngine on
# Force HTTPS
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=307,NE,L]
RewriteCond %{REQUEST_URI} ^(.*)/install.php$
RewriteRule install.php %1/install/ [R=301,L]
RewriteRule ^client/login/?$ /login.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L]

Redirect entire directory with htaccess

I can't figure out why my redirects aren't working as they are in another htaccess file.
I'm trying to redirect old urls to new ones.
RewriteEngine on
AddDefaultCharset UTF-8
DirectoryIndex index.php index.html
<IfModule mod_php.c>
php_flag magic_quotes_gpc off
</IfModule>
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule (.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
Redirect /private_3 http://website.com/new_private
I've tried all sorts of things that I've found on the web but nothing works.
Please help.
Thanks.
Instead of using this:
Redirect /private_3 http://website.com/new_private
Try adding this right below the RewriteEngine on line:
RewriteRule ^private_3(.*)$ http://website.com/new_private$1 [L,R=301]
Looks bad ordering and mixing of mod_alias with mod_rewrite might be a problem here. Try reordering your rules as this:
AddDefaultCharset UTF-8
DirectoryIndex index.php index.html
<IfModule mod_php.c>
php_flag magic_quotes_gpc off
</IfModule>
RewriteEngine on
RewriteRule ^private_3 http://website.com/new_private [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteRule ^(system.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1