Trailing slash issue with .htaccess - .htaccess

I'm using these .htaccess commands to add a trailing slash at the end of my URLs.
# Redirect To Trailing Slashes If Not A Folder Or A File...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
Its working great, however it breaks my laravel email verification because this URL
http://website.test/email/verify/12585/d3942dce589a8baf879be01b717184712b119a72?expires=1630646035&signature=59832e4888913f960ceec9c96e01d15bec742446bce834c37d4e0e8d67241c26
Becomes this URL (It adds a trailing slash at the end of the URL)
http://website.test/email/verify/12585/d3942dce589a8baf879be01b717184712b119a72/?expires=1630646035&signature=59832e4888913f960ceec9c96e01d15bec742446bce834c37d4e0e8d67241c26
👆
How to add an exception for this particular URL?
Full .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect To Trailing Slashes If Not A Folder Or A File...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# Redirect Trailing Slashes If Not A Folder...
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} (.+)/$
# RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

You can add an exception in your rule:
# Redirect To Trailing Slashes If Not A Folder Or A File...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteCond %{THE_REQUEST} !\s/email/verify/ [NC]
RewriteRule . %{REQUEST_URI}/ [R=301,L,NE]
# remove trailing / for /email/verify/...
RewriteCond %{THE_REQUEST} \s/email/verify/ [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L,NE]
Make sure to clear your browser cache completely.

Related

Double slash in url after domain name [Kohana]

I have a problem with URLs in Kohana framework.
When I type mydomain.xyz/admin that redirect to mydomain.xyz//admin and return some errors from Kohana core.
Here is a part of my .htacces:
RewriteEngine On
RewriteBase /
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|media)
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?kohana_uri=$1 [L]
###### Add trailing slash (optional) ######
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ $1/ [R=301,L]
RewriteRule ^(.*)public_html/index.php/(.*)$ http://mysite.xyz/$1$2 [R=301,L]
That problem has appeared after move site from other server.
Have it this way:
DirectorySlash Off
RewriteEngine On
RewriteBase /
###### Add trailing slash (optional) ######
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^(.*)public_html/index.php/(.*)$ /$1$2 [R=301,L,NE]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|media)
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?kohana_uri=$1 [L,QSA]
Make sure to clear your browser cache before testing this change.

How to Redirect http to https

I have this in my .htaccess file
RewriteEngine on
#
#Sub-dir e.g: /cmsms
RewriteBase /
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# but ignore POST requests.
#RewriteCond %{REQUEST_URI} !/$
#RewriteCond %{REQUEST_URI} !\.
#RewriteCond %{REQUEST_METHOD} !POST$
#RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
How can i Redirect my website from http to https and also avoid index.php?page= from the pages?
You can do:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

Mod rewrite remove trailing slash but keep on directories

So far my .htaccess is like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<FilesMatch "\.(htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$">
RewriteEngine Off
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*\.html$ index.php [L]
I would like to have url with trailing slash redirected to the one without. But the slash for directories should be kept.
Any help would be very appreciated!
UPDATE:
after some more Google research i assembled some working solution for me. Both rewriting for removing trailing slash and redirect for www are working.
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^rudolfapotheke.de$ [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
First, I would change your FilesMatch to a RewriteCond
RewriteCond %{REQUEST_FILENAME} !\.(?:htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and simplify the pattern for the next RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.html$ index.php [L]
or put the two rules together and test just for
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and finally the rule for removing the trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
To redirect those with trailing slash, you need to check if the path is a directory with a RewriteCond and then include a RewriteRule if the filename matchs /$
So it would look something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1 [R=permanent,L]

Mod rewrite conflict

I can't make redirect from http to https :'(
My actual .htaccess looks like
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
I'm adding next rows
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
But getting conflict with previous rows.
I put http://www.example.com/lents/johnson.html?type=1 to url.
And instead of getting same with https
I'm getting https://www.example.com/index.php?type=1
THANKS!
You need to put your redirect rules above all the others so the redirect occurs before any of the URL rewriting happens:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

Redirect to trailng slash (htaccess)

Trying to add trailing slash to every link. i.e. http://mysite.com/products should make 301 redirect to http://mysite.com/products/ etc. But how? Here is htaccess:
RewriteEngine on
DirectoryIndex index.php
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_URI} \.css$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/css_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_URI} \.js$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/js_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
RewriteRule /admin/(.*)$ /admin/index.php
Need help!
Here's what I'm using
RewriteEngine on
RewriteBase /
### CHECK FOR TRAILING SLASH - Will ignore files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Basically, this makes sure that it doesn't add a trailing to file and only folders or paths.
EDIT
To make it domain independent
RewriteRule ^(.*)$ $1/ [L,R=301]

Resources