Redirect to index.php and remove trailing slash .htaccess - apache

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]

Related

Force trailing slash and 301 redirect in existing RewriteRule

I have a rewrite rule that shows the contents of a directory. For example, if I go to https://www.example.com/bob/, it will show the contents of the file https://www.example.com/profile/index.php?username=bob
Here is the code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ profile/index.php?username=$1 [QSA,L]
But how can I 301 redirect https://www.example.com/bob to https://www.example.com/bob/ (force trailing slash)?
You can place this rule before above rule to force a trailing slash:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

Force trailing slash if the URL exists

I need some help to a particular redirect in htaccess. It's not a simple "force trailing slash".
So my problem is to redirect all URLs that don't have a trailing slash to a trailing slash. I accomplish this with this mod_rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|js|xml|php)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [R=301,L]
but there is a big problem doing this: if i fire for an URL that doesn't exists and doesn't have a trailing slash, Apache fires a 404 error, but for the URL that doesn't exists WITH TRAILING SLASH. So initially is redirected 301, then 404. This seems to make no sense.
How should I fix this?
Test existence first (with [OR]) and not non-existence:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !\.
RewriteRule !/$ %{REQUEST_URI}/ [NE,R=301,L]

How to remove .html and add slash in the end of url

I have been trying to remove the .html extensions, and add a trailing slash in the end of URL bar.
I have certain instance like below
So if someone enters:
example.com -- goes to-- example.com/
example.com/index.html -- goes to - example.com/
example.com/index -- goes to -- example.com/
and
example.com/first-page.html -- goes to -- example.com/main-page/ but coming as example.com/main-page
example.com/parent-page/index.html -- goes to -- example.com/parent-page/ (working)
example.com/parent-page/child-page.html/ -- goes to -- example.com/parent-page/child-page/ but comes as example.com/parent-page/child-page
Here is my .htaccess, I want add the trailing slash at the end of url.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Any one could please help me out, I would really appreciate it.
If you want to remove the .html extension from an HTML file, for example, yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to alter the last line from the code above to match the filename
Removing Extensions .html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Adding a trailing slash at the end
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
For more reference click here

.htaccess redirect /abc/def/ to /abc/def but not URLs starting with /xxx/yyy/?

I have this in .htaccess to redirect /abc/def/ to /abc/def, removing trailing slash:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
Which works well. Now I want to exclude URLs starting with /xxx/yyy/ to NOT be redirected by the above rule, e.g. /xxx/yyy/12345/ which need to stay as it is without being redirected to /xxx/yyy/12345, retaining trailing slash.
So I have:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/xxx/yyy/
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
But it doesn't work as /xxx/yyy/12345/ is still being redirected to /xxx/yyy/12345. What am I doing wrong here?
Use THE_REQUEST variable instead of REQUEST_URI as REQUEST_URI may change due to execution of other rules:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/+xxx/yyy/ [NC]
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^ %1 [L,R=301,NE]
Make sure to use a new browser for testing.
It seems it's because I have this in a .htaccess:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^ index.php [L]
RewriteRule ^(.*)$ public/$1 [L]
In one of the upper directory. So what I have to use is this:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/public/xxx/yyy/
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
Adding /public/ before /xxx/yyy/ though the public URL is actually /xxx/yyy/.

Tricky 301Redirect Rule

I want link to be redirected in a manner such that url with not slash at the end should be redirected to the url with the slash at the end.
example 1:
http://example.com/quiz/funny-riddles-with-answers
should be redirect to
http://example.com/quiz/funny-riddles-with-answers/
example 2
http://example.com/quiz/math-riddles-with-answers
should be redirected to
I have no idea how to do it.
my current screen shot of .htacess is
RewriteEngine On
RewriteBase /quiz/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ mypage.php?param1=$1&param2=$2 [L,QSA]
Try these rules in /quiz/.htaccess:
RewriteEngine On
RewriteBase /quiz/
# add a trailing slash for non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?[^/])[?\s]
RewriteRule ^ %1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ mypage.php?param1=$1&param2=$2 [L,QSA]
You can easily rewrite to a trailing slash using:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
Place above your already existing rewrites.
You need to change your current rule's pattern to: ^([^/]+)/([^/]+)?/?$
then add this right under RewriteBase /quiz/:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /$1/$2/ [L,R=301]