I would like to add trailing slash for deep level of my urls
So let's say i have this url
mysite.com/work
when user access that url, i want to be redirected to:
mysite.com/work/ (this i want to happen)
But i want this only deep levels, not for .html pages
mysite.com/testing.html/ (i don't want this to happen)
I have this .htaccess rule, but this add trailing slash to my .html pages also. i don't want that.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
Any help?
You can use this rule for adding trailing slash only for non-files:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
Related
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]
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/
I have the following htaccess rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
What this does is insert a trailing slash character to all my URLs, i.e:
http://localhost/advertising becomes http://localhost/advertising/
Now I have some URLs such as: http://localhost/contact.html
But my htaccess rule is applying the slash to these URLs too - so the above URL becomes http://localhost/contact.html/
Now I added the following condition to prevent this happening:
RewriteCond %{REQUEST_URI} !\.(html|php)$
But I want to be a bit more clever than this - I want a single expression that will match ANY extension the user types in. How can I achieve this?
This will rewrite all directories without trailing slash!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)(\..{3,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
I want to force remove the file extension.
Example: domain.com/sub/index.html
to should be automatically redirected to domain.com/sub/index [With out trailing slash]
The .htaccess code I use is not what I really want. It allows index.html to exist and if typed domain.com/sub/index it automatically redirects to domain.com/index/ [It goes to the root and adds a trailing slash]
Please help. Here is my .htaccess code
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} !(\im\.[a-zA-Z0-9]{1,5}|)$
RewriteRule (.*)$ /$1/ [R=301,L]
You need to set your base path, before any conditions are set, like so:
RewriteBase /sub
You also need to remove both slashes from your rule. So, /$1/ would become $1
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