Remove trailing slash from url using htaccess - .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.

Related

URL rewriting not working from index.php?id=2 to /2

I have a URL called
http://localhost:8080/text/index.php?id=2
and I have to redirect on
http://localhost:8080/text/2
So I added the below code in the .htaccess but it's not working
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [QSA]
I refer to the below two links. Is there any issue with my code?
common-htaccess-redirects-19-6-2018 and htaccess-rules
After suggested answer, I tried below code
HTML
Register
login
or
Register
login
.htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs. This considers that you are
hitting URL http://localhost:8080/text/2 in browser.
##Making RewriteEngine ON here.
RewriteEngine ON
##Placing conditions to check if these are non-existing pages only.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##Writing rule to rewrite to index.php with needed variable here.
RewriteRule ^[\w-]+/(\d+)/?$ index.php?id=$1 [QSA,L]
Issues in OP's attempt: You are trying to attempt to match digits in starting where your url doesn't have digits, so rather use [\w-]+ with it. Also use QSA,L flags with your rewrite rule to handle query string and come out of the rule in case this is executing.

.htaccess redirect all subdirectories to root (Kinda)

This is not as straight forward as the title might imply. I'll try to explain.
I'm currently working on a video website based on rewritten urls.
I'm using this rule currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
This is used to let users access videos with good looking urls like this:
domain.com/kJbSGe5X instead of domain.com/?v=kJbSGe5X or domain.com/index.php?v=kJbSGe5X for example.
Now the problem is that whenever a trailing slash is added, the css breaks.
I've tried solutions like adding a slash in front of the css url, like this:
<link href="/css/bootstrap.css" rel="stylesheet">
... but it's not working.
Could a solution be to rewrite all urlstrings after a trailing slash (including the trailing slash) to the same url, without the trailing string? Like this:
domain.com/kJbSGe5X/ or domain.com/kJbSGe5X/randomchars to this:
domain.com/kJbSGe5X - and how would I go about doing so?
I guess there probably is much better solutions to this problem, but I'm rather new at this.
Any help is appreciated.
Thanks in advance!
--EDIT--
I would prefer a solution where everything after a trailing slash gets redirected to the same url without the trailing slash + any string after the trailing. (If there is no content in said url)
I might have put to much emphasis on the css issue - A rule like this would work great with how my website is setup.
Insert this rule before your existing rule to remove any trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^/]+)/.*?\sHTTP/ [NC]
RewriteRule ^ /%1? [R=301,L]
You could try ignoring the css directory in your rewrite rule:
RewriteCond %{REQUEST_URI} !^/css/
This isn't exactly what you asked for but if you excluded specific directories from your rewrite rule (probably /css, /js and so on) then you would not have to worry about formatting your nice/short view URLs to remove anything after the slash or whatever else.
Here is my complete solution. With a little help from anubhava!
Remove trailing slash from all urls:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1? [R=301,L]
Constrain the "create goodlooking" urls thingy to only work when it's 8 characters (which is the length of each shortlink for videos):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.{8})$ index.php?url=$1 [QSA,L]

rewrite rule is not redirecting properly

I would like to be able to use rewrite rules to redirect anyone who opens the link :
domain.com/name
to
index.php?username=name
what would be the best way to do it?
I tried to post the htaccess code I wrote but stackoverflow keeps saying it doesn't meet standards so I removed it.
thanks in advance
Something like this will do it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* index.php?username=%1 [L,QSA]
It will map silently
http://domain.com/name
To
http://domain.com/index.php?username=name
For this to work, /name must be the first directory in the incoming URL path.

Expression Engine - htaccess to hide index.php is not working. Why?

I have an MSM install with four (licensed) sites. Three of them work perfectly and behave as they should. The fourth one, currently under construction seems to have a mind of its own. The home page shows up but attempts to add additional templates or template groups with content do not display. I only get
The requested URL /template-name/ was not found on this server.
I double checked and made sure the Enable Strict URLs was set to No and the templates are all synched properly. At this point I am repeating myself. Any clues?
Edited
I found out that if I insert index.php into the URL the other pages and templates will show, which leads me to believe that I have something wrong with the htacess file.
Here is the code I am using (which has worked just fine for other sites):
# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [L]
</IfModule>
# END ExpressionEngine Rewrite
Any clues why this is not working correctly?
Turns out it was the htaccess file. I replaced the rewrite noted above with the following and it solved the problem:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

CodeIgniter 2 no longer able to use PATH_INFO after modifying .htaccess

I'm brand new to CI and I'm trying to remove the annoying "index.php" from the URLs. This is the .htaccess I'm using:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
As you can see, I have to use QUERY_STRING to make the new URL work (note the question mark after index.php). When I try to use PATH_INFO (i.e. without the question mark), I only get No input file specified error.
This is fine, and I'm OK with using QUERY_STRING if I have to, but I don't understand the problem because I was using PATH_INFO just fine before I started playing with the rewrite - i.e. the default of "example.com/index.php/controller/function" was working, and that uses PATH_INFO AFAIK.
Does anyone know why the htaccess breaks PATH_INFO in my example? Sorry for the stupid question.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php/%{REQUEST_URI} [L]
The rewrite rules above seem to work fine for an app that I'm currently working on... they pass the correct GET requests along with the proper PATH_INFO values.

Resources