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

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?

Related

Force trailing slash except in a specific folder

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.

how to add a slash and query string after subdomain in htaccess?

I currently have a wildcard subdomain setup also using the htaccess code.
this works great
But now i also want users to able to go to
subdomain.domain.com/settings/
or subdomain.domain.com/settings
i'll need to pass a second query string to index.php
How i can do that?
Thanks.
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com
RewriteRule ^$ /index.php?subdomain=%1 [L]
tagert url : /index.php?subdomain=%1&page=settings
"settings" would be replaced by what user enters, it could be login or register or dashboard. That would be the page to go to.
If it is acceptable for index.php to receive an empty value for page, you can merely change the one RewriteRule you have to accommodate it, expecting that if not supplied, $_GET['page'] will be present as an empty string:
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com
# Don't send index.php into a rewrite loop
RewriteCond %{REQUEST_FILENAME} !-f
# Capture page in $1, everything up to the first / or the end $
RewriteRule ^([^/]*)/?$ /index.php?subdomain=%1&page=$1 [L]
The pattern ([^/]*) will match anything up to the first /, if there is anything present. The /? allows for an optional trailing slash. Given this pattern, any of these would match, but the final two would result in an empty $_GET['page']
subdomain.example.com/settings
subdomain.example.com/settings/
subdomain.example.com/
subdomain.example.com

Add a trailing slash mod_rewrite

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.

Hide file extension is not working htaccess

I am using mod_rewrite to hide file extension this code is working prefect but when i add slash at the end of url it's show error 500 internal server error
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Please help me
but when i add slash at the end of url it's show error 500 internal server error
This is because the slash is being grouped together in the (.*) and then referenced by $1, so a request like:
http://your.host.com/my_file/
is getting rewritten to:
http://your.host.com/my_file/.php
The reason is that the condition %{REQUEST_FILENAME}\.php exists, because the trailing slash gets ignored. You just need to include the match against the trailing slash in your RewriteRule's regex:
RewriteRule ^(.*?)/?$ $1.php

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.

Resources