How to automatically remove trailing slash in .htaccess file - .htaccess

I would like to remove trailing slash automatically. When i insert slash at the end of the URL the page leads to Google search.Here my .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*) index.php/$1 [L,QSA]

Kindly try the code below. Insert it below RewriteEngine On.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]`

Related

Why htaccess isn't working with pretty links

Here's my current code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /go/redirect.php?slug=$1 [L]
This file is located at http://example.com/go/.htaccess
It is working correctly when people visit this link:
http://example.com/go/test
But not when it has a trailing slash like this:
http://example.com/go/test/
When the trailing slash exists, they are being redirected here for some reason:
http://example.com/test
How can I make this work with and without trailing slashes at the end of the URL?
Try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /go/redirect.php?slug=$1 [L]

How to write htaccess in this case

this is my .htaccess
RewriteEngine On
RewriteCond $1 !^(index\.php|public)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(acp|acp/)$ /acp/home.html [L,R=301]
RewriteRule ^(.*).html$ index.php/$1 [QSA,L]
But when i upload html file into root folder (example: ads.html) and run http://domain.com/ads.html it's not work, how to fix it
Conditions only work for the rule they appear before. Your conditions work for the rule that rewrites acp to acp/home.html. You want to move the conditions directly before the second rule. You would end up with:
RewriteEngine On
RewriteRule ^(acp|acp/)$ /acp/home.html [L,R=301]
RewriteCond $1 !^(index\.php|public)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ index.php/$1 [QSA,L]

Creating an htaccess for .py files

How might I create an .htaccess so that it replaces any www.w.com/cgi-bin/x.py with www.w.com/x/
So basically, it replaces /cgi-bin/$x.py with /$x and adds a trailing slash to make it /$x/
Thanks!
You can use:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+cgi-bin/([^.]+)\.py [NC]
RewriteRule ^ /%1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ /cgi-bin/$1.py [L]

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]

site loading for invalid URL's and redirection error

currently I am using this htaccess access,but the site still loads for invalid url, it doesn't goto 404.
ex: http://www.couponcoder.in/babyoye.com -->works and main content
http://www.couponcoder.in/babyo ----> invalid url, it displays homepage
and site loads for both slash and without slash, I just want it to redirect non-slash
www.couponcoder.in/babyoye.com/
www.couponcoder.in/babyoye.com
can someone help out with this?
Options +Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?couponcoder\.in$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1couponcoder.in%{REQUEST_URI} [R=301,L]
RewriteRule ^admin$ Admin/index.php?qstr=$1 [L]
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]
I can't read if you want to have the non-slash version or if you want to redirect the non-slash version to the slashed one. I'm assuming you want the former.
...
#remove slash, and redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]

Resources