Rewrite condition in .htaccess for slash and non slash version throws status code 500 - apache

We are trying to set up a redirection rule in .htaccess so our every URL with slash in the end is redirected to a non slash version.
We have implemented this code which i believe should do the work:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
What is happening now is, when I try to visit the slash version, I get 500 status code. Here is the URL of the website: plicnivek.cz. Is our .htaccess rule implemented correctly?

Your rule is incorrect as it also rewrites non-existent files directory to PHP. You can use the following rule to remove trailing slash and rewrite php extension.
RewriteEngine on
#remove traling slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [R=301,L]
#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+)/?$ /$1.php [L]

Related

.htaccess - remove everything after third slash in path

On my website, I only use 3 slashes in my URL path:
https://example.com/this/isatest/
Right now I use .htaccess which makes it possible (as a side effect) to add as many stuff on the URL as you like:
https://example.com/this/isatest/hipperdihopperdus/pizza/bacon/with/cheese
I'd like to automatically remove everything after "isatest" while keeping the trailing slash using .htaccess.
This is what my .htaccess currently looks like:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^index\.html$ /? [R=301,L,NC]
RewriteRule ^listen/$ /console/ [NC,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 [L,QSA]
How can I achieve this?
As your first rule, after the RewriteEngine directive, you can do something like the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]
This checks if there is anything else (the dot) after two path segments and a slash, and redirects to removed "anything else".
Note that this is a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect - if that is the intention - once you have confirmed that it works OK. This is to avoid the browser caching erroneous redirects whilst testing.
UPDATE: It may be more efficient to simply avoid redirecting files that end in a recognised file extension. Or perhaps exclude known directory location(s) of your static resources. For example:
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif)$ [NC]
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]
OR,
RewriteCond %{REQUEST_URI} !^/static-resources/
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]
You can add this rule just below RewriteEngine On line:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/[^/]+/).+$ /$1 [R=301,L,NE]

Apache mod_rewrite - adding trailing slash to existing file creates a problemo

This is my existing .htaccess script
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule (.*) $1.php [L]
Everything works fine except when I add a trailing slash at the end of a url, I get 500 error.
i.e. http://example.com/dir/file works, but not
http://example.com/dir/file/ --> error 500 (redirected too many times) instead of error 404.
Can someone pls help me out?
Many thanks in advance.
I figured it out: write 2 conditions.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [NC,L]
Capture the trailing slash and redirect to the file without trailing slash. If no trailing slash found, then simply add .php (or other extension) to it.

Mod rewrite htaccess - two variables in rewrite rule

I found this solution online:
RewriteEngine On
RewriteBase /test/
RewriteRule ^([^-]*)/$ index.php?page=$1
RewriteRule ^([^-]*)/([^-]*)/$ index.php?page=$1&link=$2 [L]
#dodaje slash na koncu
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
The first one RewriteRule works perfect, it returns me $_GET['page']=130. But when it comes to second one, it returns me $_GET['page']=index.php instead of $_GET['page']=130 and $_GET['link']=35. That finish with SQL error, because of numeric id of page.
Normal link looks like:
?page=136
?page=136&link=35
Rewrited one:
/136/ - works
/136/35/ - doesn't work, $_GET['page']=index.php
You can replace your current code by this one (your htaccess has to be in test folder, and it's the same for index.php)
RewriteEngine On
RewriteBase /test/
# add trailing slash if no trailing slash and not an existing file/folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ $1/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/$ index.php?page=$1&link=$2 [L]
RewriteRule ^([^/]*)/$ index.php?page=$1 [L]
You can try these links
http://domain.com/test/136/35/ internally rewrites to index.php?page=136&link=35
http://domain.com/test/136/35 redirects to http://domain.com/test/136/35/
http://domain.com/test/136/ internally rewrites to index.php?page=136
http://domain.com/test/136 redirects to http://domain.com/test/136/

Remove trailing slash if not a directory with apache

I have the following rewrite rules:
#remove the www.
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ http://local.website.co.uk/$1 [R=301,L]
#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
I want to add in a rule that removes the trailing slash if someone tries to access site with one.
eg
website.co.uk/cheese/ should redirect to /cheese
as you can see I have a rule that redirects ursl with the .php extention, not sure where to begin.
I do have directory in the root folder which I do not wish to remove the trailing url, but I can add a ignore rule for those.
Cheers
Make the change below to your .htaccess file
RewriteEngine on
RewriteBase /
#existing rule
#remove the www.
RewriteCond %{HTTP_HOST} ^www.website.co.uk$ [NC]
RewriteRule ^(.*)$ http://local.website.co.uk/$1 [R=301,L]
#new Rule
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]
# rest of your existing rules go here

trailing slashes in 301 redirect

I've tried to look through the multiple mod_rewrite questions, so I apologize if this is a duplicate.
I'm trying set it so that if you go to domain.com/about.php it removes .php and if you go to domain.com/about it simply remains like that.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /$1.php [L,QSA]
RewriteRule ^(.*)/$ /$1 [L,R=301]
So, right now if you go to domain.com/about it displays the page, but if you go to domain.com/about.php it doesn't remove the extension.
Additionally, I have 301 redirects
redirect 301 /our-clients http://www.domain.com/about-ourclients
That works perfect, but if the user goes to domain.com/our-clients/ with the trailing slash, they are directed to about-ourclients.php
Any advice on how to rewrite my rules?
This should do the job:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
It will redirect all direct requests to php files: /something.php will be redirected to /something
Will remove the trailing slash IF requested resource is not directory. So if you requesting /home/ and you do have such folder, then it will NOT be redirected to /home.
Will internally rewrite requests to the same named PHP file IF it does exist. If you are requesting /about and you have /about.php then it will do rewrite; If you have no /about.php then nothing happens (well, at least not on these rules -- if you have more rules then such request can be matched later .. or 404 error page will be shown).
If you are requesting /about, you have /about.php and you also have /about folder, then request will go into folder. If you do not want this to happen ( /about should always be rewritten to /about.php) then you need to remove RewriteCond %{REQUEST_FILENAME} !-d from last block. But since you have exactly the same condition in your current .htaccess then I assume it is desired behaviour.