Add a trailing slash mod_rewrite - linux

just wondering how I add a trailing slash at the end of my URLs using Mod_Rewrite?
This is my .htaccess file currently:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?pageName=$1
My URL show like so:
wwww.******.com/pageName
I want it to show like so:
wwww.******.com/pageName/
The URL is holding a GET request internally, but I want it to look like a genuine directory.

You can easily add a trailing slash to the URL that a client sees, but you'll have to take into account that trailing slash in requests that you get after you've redirected the browser.
So the redirect could look something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
And you'll want it above the:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?pageName=$1
ruleset, because the redirect needs to happen before the routing to index.php. Note that when the browser gets redirected to a URL that ends with a /, your index.php rule will have the pageName param with a trailing slash in it.

I know this is old, but it's still useful for others that find it.
Jon Lin's answer is correct, I would add this as a comment but I don't have enough reputation.
The OPs comment about removing the trailing slash, since you know it will always have a slash at the end, you could $pageName = substr($pageName, 0, -1);. That's what I'm doing at least.
Hope this is useful.

Related

How to clean url to accept ending slash or not with .htaccess

How do I clean URL
http://localhost/mysitedirectory/contacts.php
to look like both:
http://localhost/mysitedirectory/contacts
http://localhost/mysitedirectory/contacts/
I want this because a user may add the ending trailing slash or leave it so I want .htaccess to configure such that it works both ways.
I noticed when I add the / at the end of the contacts it says "OBJECT NOT FOUND"...Error 404
How do I make it work even if I add the slash or not?
this is what I tried:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^contacts$ contacts.php [L,QSA,NC]
RewriteRule ^home$ index.php [L,QSA,NC]
CBroe answered in a comment section:
^contacts/?$ - slash at the end, made optional by the following quantifier question mark.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^contacts$ contacts.php [L,QSA,NC]
RewriteRule ^home$ index.php [L,QSA,NC]
The two RewriteCond directives (filesystem checks) are entirely superfluous and should be removed. (As are the QSA flags on the RewriteRule directives.)
However, instead of allowing both /contacts and /contacts/ (strictly speaking two different URLs) resolve to the same content (which would necessitate the need for a rel="canonical" tag) you should externally redirect from one to the other (canonical) URL.
By the look of it, the URL without the trailing slash is canonical, so remove the trailing slash with an external redirect. The only thing to be careful of is that you should not remove the trailing slash from physical directories (which would ordinarily cause a redirect loop).
For example:
Options -Indexes -MultiViews
RewriteEngine On
# Canonical redirect to remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ /$1 [R=301,L]
# Internal rewrites
RewriteRule ^contacts$ contacts.php [L,NC]
RewriteRule ^home$ index.php [L,NC]
The NC flag on the RewriteRule directives makes contacts, CONTACTS and CoNtAcTs all resolve to the same content. (So the rel="canonical" tag is still required to resolve this ambiguity.)

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]

HTaccess - Trailing slash + .php + anchors (ExpressionEngine)

I just started a project from another webmaster work (i hate patching other's stuff ::sad::) and i have some issues. First we are on expression engine 1.x .
My problem: There is a trailing slash redirection in the .HTACCESS, but my users need to have access to only one .php page (www.mydomain.com/mobile/index.php) but the link is redirected to /index.php/, another problem is the anchors are changed to the same way (www.mydomain.com/somepage/#anchor1) to /#anchor1/
So my question is... there is a way to put exception into trailing slash redirection code? I mean i just have to fix it in few pages. Take note that our expression engine remove all the index.php to have links like www.mydomaine/contact/, www.mydomaine/about/, www.mydomaine/infos/ , ect.
Currently the htaccess trailling code is :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
P-S: we have a code that remove index.php too:
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
Thx for help!
Is there a valid reason trailing slashes in URLs need to be enforced? If not, I would just remove the rewrite rule and your problem is solved :)
Most ExpressionEngine URLs will work with or without trailing slashes.
The easiest fix to this problem would be remove the overzealous trailing slash redirection, and replace it with a more relaxed version:
<IfModule mod_rewrite.c>
# Enable Apache's RewriteEngine
RewriteEngine On
# Add Trailing Slashes to URLs
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [R=301,L]
# ExpressionEngine Remove index.php from URLs
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Using the above code, example.com/mobile/index.php will not get rewritten to example.com/mobile/index.php/, nor will pages with anchors example.com/page/#anchor1.
EE doesn't know the difference between URIs like /mobile versus /mobile/, web analytics apps and search engines may consider these separate web pages. If you're developing a static website, this isn't a big deal, because if you attempt to go to the former URI (sans trailing slash), Apache will automatically redirect the client to the latter (with trailing slash).
But for a web application like EE, where everything in the URI after index.php is handled by the application rather than Apache, this redirection is left up to you. Just like the decision to use or not use a www subdomain, it doesn't matter whether you choose to force a trailing slash or vise-versa; it just matters that you enforce one or the other.
Sidenote: in EE1, trailing slashes are generated in URLs produced by
ExpressionEngine; in EE2, trailing slashes are not generated. The exception
is the Structure Module, which outputs URLs with trailing slashes
in both EE1 and EE2.

htaccess directory to file redirect problem

I’m trying to use the following .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^images/
RewriteRule (.*) view.php?picid=$1 [L]
RewriteRule ^/user/(.*)$ /users.php?user=$1
I want two things to happen: Whenever someone requests /1234, it redirects to /view.php?picid=1234, and also when someone visits /users/bob, it redirects to /users.php?user=bob.
My code however, doesn’t seem to be working correctly.
There are several ways to do that. Here’s one that should work:
RewriteRule ^user/(.+)$ users.php?user=$1 [L]
RewriteRule ^([0-9]+)$ view.php?picid=$1 [L]
The first rule will catch any request that’s URI path begins with /user/ followed by one or more arbitrary characters. And the second will catch any request that’s URI path begins with / followed by one or more digits.
The initial problem with your rules is that the RewriteRule with (.*) will match everything.
If you do not want it to match a URL with a slash in it (such as users/bob), try ^([^/]*)$
Secondly, after a URL is rewritten, the new URL goes through your rules again. If you want to avoid matching something that has already been rewritten once, you should add a condition like
RewriteCond %{REQUEST_URI} !\.php

Resources