Force trailing slash except in a specific folder - .htaccess

I am forcing trailing slashes on my website but it's caused an issue with our Wiki, as it treats them as a blank page.
Want I want to do is force trailing slash except if in /wiki/. How would I do this?
Here is my current rule for it:
# force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]

You can add a RewriteCond (condition) directive to create an exception for any URL that starts /wiki/.
For example:
# force trailing slash
RewriteCond %{REQUEST_URI} !^/wiki/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
The ! prefix on the regex ^/wiki/ negates the expression, so it is successful when it does not match.
You will need to clear your browser cache.

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.)

.htaccess rewrite blank spaces with - ("%20" to "-")

I want to remove %20 on my link to - (dash),
My .htaccess is like that right now,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ icerik.php?name=$1 [QSA,L]
For example,
site.com/vision-and-mision,
site.com/do-re-mi-fa-so-la-si.
Actually I searched something but the informations were very specific and I'm confused
Thank you.
You can use the following in your /.htaccess file:
RewriteEngine On
# Replace whitespace with hyphens, set the environment variable,
# and restart the rewriting process. This essentially loops
# until all whitespace has been converted.
RewriteRule ^([^\s]*)\s(.*)$ $1-$2 [E=whitespace:yes,N]
# Then, once that is done, check if the whitespace variable has
# been set and, if so, redirect to the new URI. This process ensures
# that the URI is rewritten in a loop *internally* so as to avoid
# multiple browser redirects.
RewriteCond %{ENV:whitespace} yes
RewriteRule (.*) /$1 [R=302,L]
Then add your rules afterwards:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /icerik.php?name=$1 [QSA,L]
If this is working for you, and you would like to make the redirects cached by browsers and search engines, change 302 to 301.

Removing trailing slash working great, until a query string is present

Basically, I have a rewrite rule, that removes the trailing slash from a URL. I'm currently using IIRF (IIS 6), and the rule will take the following URL and remove the slash like so:
http://test.site.com/home-search/communities/state/city/
and successfully changes it to:
http://test.site.com/home-search/communities/state/city
but when a query string is there, it automatically, adds the trailing slash back in. So, basically adding ?param1=xx&param2=yy to the URL, re-adds the slash before the query string.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} $ !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R=301,QSA]
If I add the following rule, I get a redirect loop that breaks the page, but does remove the trailing slash properly.
RewriteRule ^(.*)\/(\?.*)?$ $1 [R=302,QSA]
Any idea what I could possibly be doing wrong?

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 with or without slash

What do I need to do to the following rewrite rule to make it so it works whether or not their is a slash at the end of the URL?
ie.
http://mydomain.com/content/featured
or
http://mydomain.com/content/featured/
RewriteRule ^content/featured/ /content/today.html
Use the $ to mark the end of the string and the ? to mark the preceding expression to be repeated zero or one times:
RewriteRule ^content/featured/?$ content/today.html
But I recommend you to stick to one notation and correct misspelled:
# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]
# add trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ $0/ [L,R=301]
simple way to do this :
RewriteEngine On
RewriteBase /
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC]

Resources