How to add trailing slash to url? - .htaccess

I want that
http://www.example.com/contact
always gets redirected to
http://www.example.com/contact/
with ending slash.
This is my current simple url rewrite setting:
RewriteEngine On
RewriteBase /
RewriteRule ^([^.]+)$ index.php?page=$1

Try the following:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}/$1/index.html -f
RewriteRule ^(.+?)/?$ /$1/index.html [L]

Related

How to remove the need of txt extension with a final optional trailing slash in url using htaccess

How to remove the need of txt extension of txt files and add a final optional trailing slash in url using htaccess regex in php
my code is
#turn on url rewriting
RewriteEngine on
#remove the need for .txt extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)(/?)$ $1.txt
But it throws 500 Server error.What's wrong with the code?
Based on your shown samples, could you please try following, please make sure you clear your browser cache before testing your URLs. Fair warning I haven't tested it as of now.
RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/$1\.txt -f
RewriteRule ^(.*)/?$ $1.txt [L]
EDIT: As per OP's comments adding following solution.
RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.*)/$ $1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/$1\.txt -f
RewriteRule ^(.*)/?$ $1.txt [L]

.htaccess Rewrite to Force Trailing Slash after remove .html

I have the following code in my htaccess file:
Options -Indexes
RewriteEngine On
# Redirect external .php requests to extension less url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}/foo/$1 [R=301,L]
# Resolve .html file for extension less html urls
RewriteRule ^([^/.]+)$ $1.html [L]
That work fine when I go to www.mydomain.com/foo/index whithout .html. I want force trailing Slash at the end of index but i don't know how
Does anyone know how to modify my code to make the trailing slash work ?
Thanks!
To add a trailing slash to your extension less html urls, You need to modify your both of the rules.
In the first rule, we add a trailing at the end of the target url so that a slash is added to all .html requests.
and in the second rule we add a trailing slash at the end of the regex pattern to accept a request with a trailing slash.
Options -Indexes
RewriteEngine On
# Redirect external .php requests to extension less url
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ http://%{HTTP_HOST}/foo/$1/ [R=301,L]
# Resolve .html file for extension less html urls
RewriteRule ^([^/.]+)/?$ $1.html [L]
Clear your browser's before testing this!
(Hope,this helps!)
I modified my code for this:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/fr/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://%{HTTP_HOST}/fr/$1/ [R=301,L]
It works well

.htaccess rewrite rule giving server error

I think I just need a second pair of eyes for this as I can't see why i'm getting a server error.
RewriteEngine On
RewriteRule ^Gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^Gig/([a-zA-Z0-9_-]+)/$ gig.php
#allow non caps
RewriteRule ^gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^gig/([a-zA-Z0-9_-]+)/$ gig.php
Edit:
I have now viewed the log and the reason is there are far too many internal redirects. I myself am not too competent at mod_rewrites etc so please have a look.
#redirect so home page shows /Home
Redirect /home.php http://localhost/Home
#add php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Redirect /home.php http://localhost/Home
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.*)\.php /$1 [R=301,L]
#redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.^([a-zA-Z0-9_-]+)$ [NC]
RewriteRule ^(.*)$ http://^([a-zA-Z0-9_-]+)$1 [L,R=301]
#remove trailing slash
RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
#allow artistprofile nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ artist_profile.php
#info nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Aa]bout/?$ artist_about.php
#gigs nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Gg]igs/?$ artist_gigs.php
#tracks nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Tt]racks/?$ artist_tracks.php
#gig nice url
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
That's all the rewrites in the .htaccess file
Edit:
The problem is the adding PHP extension part
#add php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I think its due to the ([a-zA-Z0-9_-]+) being after the file name.
You need to escape your - characters when they're inside brackets. You can also shorten your code to the following:
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
# Escape it! ^^
Why can we shorten it like this?
[Gg] means "The character G or g"
/? means "/ repeated 0 or 1 time"

force pages to end in a slash and rewrite the url

i have to call my pages in this way:
http://example.com/index.php?page=homepage
i want to always show the url in this way
http://example.com/homepage
and at the same time i also want to prevent the insertion of the / at the end of the url...so:
http://example.com/homepage/ or http://example.com/homepage
point to the same page.
how can i do this trick with htacces? have i to correct all relative path of files in my html?
thanks a lot
# activate Rewrite Engine
RewriteEngine On
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# remove trailing slash
RewriteRule ^(.+)/$ $1 [R=301,L,QSA]
# rewrite all other requests to index.php
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
RewriteEngine on
RewriteBase /
RewriteRule ^(css|js)/(.*)?$ $1/$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
RewriteRule ^(.*)/$ $1 [R]
Last update from this.

.htaccess mod_rewrite question

If I have a url http://example.com/page.php
but I want the page to display when someone goes to http://example.com/page/
how do I go about doing that?
Try this:
RewriteEngine on
RewriteRule ^([^/]+)/$ $1.php
I usually use something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^$ index.php [L,QSA]
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
</IfModule>
Note the question mark after the slash. You could add that to Gumbo's example to "test" for a trailing slash (so it can be there or not).

Resources