How to remove / at the end of url by htaccess - .htaccess

Although I have tried hard, I could not be able to remove the auto-added "/" signs at the end of URLs. Please kindly help me. I can send the .htaccess file content of my site. I could not paste here. It gives error. Is there a way to attach it here for you?
Thanks in advance

To enforce no trialing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Htaccess: add/remove trailing slash from URL

Related

Remove trailing slash from url using htaccess

i have the following rewrite condition and it works fine:
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|asset|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
However, all it takes is adding a trailing backslash to my url and the site breaks. I have seen several similar questions and answers but nothing works for me, mainly because i'm not familiar with htaccess.
How can i make make my url auto change from
http://localhost:8888/mysite/mylink/
to
http://localhost:8888/mysite/mylink
Iam using codeigniter and my application is still in localhost. Appreciate any help in this. Thanks.
Just change all your links without that slash at the end.

htaccess file to tidy up urls causes 500 server error if a trailing slash is added to the url

Hi please can anyone help,
I've written an htaccess file to tidy up my urls & remove the .php. It all seems to work well but if a trailing slash is added onto a url now the .php has gone I get a 500 server error. What I really want to happen is if a trailing slash is added then it's handled smoothly and redirected to the no trailing slash option. This seems to work on the homepage, but not on any other page.
My website is www.bekcruddace.co.uk & the htaccess file is:
RewriteEngine on
#Redirects example.com to www.example.com
RewriteCond %{HTTP_HOST} ^bekcruddace.co.uk$
RewriteRule ^/?$ "http\:\/\/www\.bekcruddace\.co\.uk\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Please could anyone offer any pointers?
I should also mention that the news sections is a separate plugin that runs independently so the url is slightly different & isn't relevant to my question.
Thank you.
If you request /file/ your rule rewrites it to an invalid location /file/.php . That is why your server is producing a 500 error.
You need to make the traling slash optional in your pattern so that the slash is excluded from the capture group:
RewriteRule ^(.*)/?$ $1.php

Htaccess, urlrewriting and subdomain, how to had an exception?

I am working on this website:
www.montreuxjazz.com
There, we put a htaccess to rewrite the urls:
RewriteRule ^([^\/]*)/([^\/]*)$ index.php?pageid=$1&newsid=$2 [L]
Unfortunately, this htaccess avoid the access to http://newsletter.montreuxjazz.com/noel2011_en.html .
Do you know how to modifiy the htaccess so that everything which is on newsletter.montreuxjazz.com/ become accessible?
Thank you in advance for your help!
See you,
David
What about only processing your rule when it would otherwise take you to a nonexistent path?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/])/([^/])$ index.php?pageid=$1&newsid=$2 [L]

URL rewriting whilst using codeigniter

I am using the following to remove my index.php in codeigniter -
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I am replacing an old site with my new one written with codeigniter, because of this I need to rewrite some old urls to new ones. This is working fine for single pages, however I am running into a problem with the following rewrite -
RedirectMatch 301 ^/comments/(.*)/$ /location/$1
This should in theory redirect you from:
http://www.mysite.com/comments/123 to http://www.mysite.com/location/123
Because I’m removing index.php through a rewrite I am ending up being directed to -
http://www.mysite.com/location/123?/comments/123/
Does anyone know how to fix this?
Thanks.
You should not mix mod_alias (RedirectMatch) and mod_rewrite. Try this mod_rewrite rule instead:
RewriteRule ^comments/(.*)/$ /location/$1 [L,R=301]
Now just make sure to put this rule in front of your other rule.
Finally got it, for those that are interested, I had to add comments to a rewrite condition that I failed to mention in the first post:
RewriteCond $1 !^(index\.php|media|images|css|js|comments|robots\.txt)
Hopefully this may help somebody else.

How can I force some pages to end in a slash [.htaccess]

This is a multi-site problem. I have a lot of sites with .htaccess files with multiple line similar to:
rewriterule ^(page-one|page-two|page-three)/?$ /index.php?page=$1 [L]
This means that both www.domain.com/page-one and www.domain.com/page-one/ will both load www.domain.com/index.php?page=page-one
However I'm always told that it is good SEO practice to make sure you use only one URL per page so what I'd like to do make www.domain.com/page-one to redirect to www.domain.com/page-one/ via the .htaccess file.
Please note the answer I'm NOT looking for is to remove the ?$ from the end of the line as that will just cause www.domain.com/page-one to become a 404 link.
Here's a snippet to force everything to end with a slash
rewritecond %{REQUEST_FILENAME} !-f
rewritecond %{REQUEST_URI} !(.*)/$
rewriterule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
Use an HTTP redirect to send users who use the "wrong" URL to the correct one. You could for example use:
RewriteRule ^(.+[^/])$ $1/ [R]
before your own RewriteRule.
Beware, though: this will also add slashes to URLs that end in a filename. If you have such URLs, you have to put exceptions for them before the redirect.
See also the FAQ entry on trailing slashes and the example page for mod_rewrite!
Untested, but can't you just do this?
RewriteRule ^(.*[^/])$ $1/
RewriteRule ^(page-one|page-two|page-three)?$ /index.php?page=$1 [L]

Resources