Force trailing slash in .htaccess - .htaccess

This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Force https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Header set X-UA-Compatible "IE=Edge,chrome=1"
# block text files in the content folder from being accessed directly
RewriteRule ^content/(.*)\.(txt|md|mdown)$ error [R=301,L]
# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
</IfModule>
What can I do to force a trailing slash when there isn't one? For instance, my current URLs look like this:
https://amemoirproject.com/chapters/quarter-after
I'd like to force a trailing slash so they look like this:
https://amemoirproject.com/chapters/quarter-after/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]
The RewriteCond makes sure that there isn't any files with that name. If there is not, it adds the trailing slash.

Related

Allowing a trailing slash for a specific dynamic url?

I have done a 301 redirect for all trailing splash URLs in my htaccess file. Now I have a few dynamic URLs `website.com/item/activate/#dynamiclychangingvalue**/** that need the splash. Note that in the end on the activate folder is a dynamic url that needs the splash.
How would I edit the .htaccess file? I have tried all sorts of codes, they dont seem to be working.
IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
the dynamic url looks like this website.com/item/activate/#dynamiclychangingvalue**/**
You can use:
RewriteCond %{REQUEST_URI} !^/additem/ [NC]
RewriteRule ^(.+)/$ $1 [R=301,L]

301 Redirect won't work in Laravel

Lets say I'm trying to do a 301 redirect to /housing-associations from /table/09 - the following code is not redirecting. I'll include the whole .htaccess file. It's a Laravel installation. The other redirects work ie all urls are being directed to index.php and trailing slashes are being removed:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^fabric [NC,OR]
RewriteCond %{HTTP_HOST} ^www.fabric [NC]
RewriteRule ^/table/[0-9]{2} /housing-associations [R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Has anyone got any ideas. I cannot work out why this won't work?
Thanks
This should work for you:
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?fabric [NC]
RewriteRule ^table/[0-9]{2} /housing-associations [R=301,L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The two HTTP_HOST conditions are merged into one, where the www. is optional. The leading slash from /table has been removed. Lastly, the L flag was added to prevent the index.php rule from running. If the L flag is not there, you'll get a Moved Permanently message from Apache.

.htaccess 301 Redirect non slash domains and slash domain to .html

I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html

.htaccess pretty URL's excluding index.php and adding trailing / to the end

Ok, I've fought with it for hours. I have 3 different .htaccess scripts which do what I need, but I'm unable to mix them together.
Make a pretty url from (example.com/gallery.php -> example.com/gallery)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ $1.php
The script from #1 though forwards example.com/index.php to example.com/index, so this code removes index.php so example.com/index.php -> example.com
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
The script should add a trailing slash so example.com/gallery -> example.com/gallery/
# invoke rewrite engine
RewriteEngine On
RewriteBase /~new/
# add trailing slash if missing
rewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]
Can someone help me to combine those 3 scripts into one universal pretty URL scripts
add these directives to .htaccess in the root directory of your website
Options +FollowSymLinks
RewriteEngine On
# add trailing slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# rewrite gallery/ to gallery.php
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
# redirect example.com/index.php to example.com/
RewriteCond %{REQUEST_URI} index\.php$
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]

.htaccess redirect according to url

In my hosting account, I have domains. One is located at the root and the other is in the /example/ folder. Here is the redirect code in my main .htaccess file, which works fine apart from the function I just described. This function was working previously, and has mysteriously stopped-
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/example(.*)$ http://example.com/$1 [R=301,L]
# Rewrite /example to http://example.com
# Remove .php from file names and force added slash
# http://stackoverflow.com/questions/1068595/htaccess-code-to-remove-extension-and-addforce-trailing-slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
Any ideas?
Try removing the leading slash, like this:
RewriteRule ^example(.*)$ http://example.com/$1 [R=301,L]

Resources