Apache RewriteRule matching ^abc/?(.*)$ and thus missing ^abcd/?(.*)$ - regex

I have a programmatically generated set of rules for a site like this:
RewriteEngine on
RewriteRule ^abc/?(.*)$ /$1?organisation=abc [QSA,L]
RewriteRule ^fghij/?(.*)$ /$1?organisation=fghij [QSA,L]
RewriteRule ^fghijklmn/?(.*)$ /$1?organisation=fghijklmn [QSA,L]
So that our client can set up multiple minisites on their domain for various clients of their own.
Because the url could end in / or /blah.php I've created the rules as above but this means the RewriteEngine would stop after ^fghij/?(.)$ and never find ^fghijklmn/?(.)$
How could I rewrite my rules so every organisation is properly matched?
Many thanks in advance

Your regex seems to be a problem since ^fghij/?(.*)$ would also match fghijklmn.
Try this code:
RewriteEngine on
RewriteRule ^abc(/.*)?$ $1?organisation=abc [QSA,L]
RewriteRule ^fghij(/.*)?$ $1?organisation=fghij [QSA,L]
RewriteRule ^fghijklmn(/.*)?$ $1?organisation=fghijklmn [QSA,L]

I think that the /? in ^abc/?(.*)$ is saying optionally match / then match anything else after it so you likely want something like
RewriteRule ^abc//?(.*)$ /$1?organisation=abc [QSA,L]
That will enforce a / at the end since you say the URL will always end in / or /blah.php
Also, I am unsure why you don't just try to match the organisation with a non-greedy matcher on the word before the first slash and then append it to the query string instead of creating it manually.

Related

Apache RewriteRule with ampersand in new URL

I tried to rewrite this URL
https://www.test.com/blog/post/41/postname.html
to
https://www.test.com/content.php?coId=11&postId=41
by
RewriteCond %{REQUEST_URI} (.*)\/blog\/post\/([0-9]*)\/(.*)\.html$
RewriteRule (.*) /content.php?coId=11&wpPostId=%2 [QSA,L]
However, the ampersand in the RewriteRule is probably not correctly escaped and I have not found any solution how to do this. According to the htaccess tester at https://htaccess.madewithlove.be it is converted to
https://www.test.com/content.php?coId=11416wpPostId=41
which means that the ampersand is replaced by '416'.
You may use this simple rule for this:
RewriteRule ^blog/post/([^/]+)/[\w-]+\.html$ index.php?coId=11&wpPostId=$1 [QSA,L,NC]
Make sure to test it in a new browser to avoid old browser cache.

Remove multiple trailing slashes in root using htaccess

I have a rule in my htaccess file to remove any extra trailing slashes from a url, this works on sub-directories with any more than 1 trailing slash. However it doesn't work on the root; which i need it to do.
For example.
http://www.example.com/test//// Redirects to http://www.example.com/test/
http://www.example.com/// Needs to redirect to http://www.example.com
Any ideas on what i need to add?. Cheers.
RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . %1/ [R=301,L]
For removing multiple slashes anywhere in REQUEST_URI this rule works best:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=301,L,NE]
It takes advantage of the fact that mod_rewrite engine itself converts all multiple forward slashes to a single slash in the RewriteRule pattern. We use RewriteCond %{THE_REQUEST} to make sure original REQUEST_URI contains multiple slashes.
Here [^?]*// matches 2 // before matching query string since [^?] matches anything except ?. This will allow // in query string.
Try with:
RewriteCond %{REQUEST_URI} ^(.*?)//+$
RewriteRule ^ %1/ [R=301,L]
You htaccess works great as you can test on below link
https://htaccess.madewithlove.be/
So you need to make sure you test either with a Chrome Incognito window or using like below
curl -v http://example.com////
I usually prefer curl as I know it will give a fresh response from the server always
You just need two rule to match two different pattern
RewriteCond %{REQUEST_URI} ^(?:/){2,}$
RewriteRule . / [R=301,L]
RewriteCond %{REQUEST_URI} ^(.*?)(?:/){2,}$
RewriteRule . %1/ [R=301,L]

Remove dot and parentheses from the URL in rewrite rule

I have the a rewrite rule where I am trying to remove any dots and parentheses from the URL and I managed to get get to the point where I can do one or the other but just can't seem to combine both. How would I write the rule so that if any url has a dot or parentheses, they get removed? Thanks.
RewriteEngine On
RewriteCond %{REQUEST_URI} [\(\)\.]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ /$1$2$3 [L,R=301]
Try:
RewriteRule ^(.*)[().](.*)$ /$1$2 [E=DOT:Y,DPI]
RewriteCond %{ENV:DOT} Y
RewriteRule ^([^().]+)$ /$1 [L,R=301]
This uses an environment variable as a way to know when to redirect. Any number of parentheses and dots will get removed and when they're all done, the URL gets redirected.

htaccess special character RewriteRule not working yet

We have a bunch of URLs that were indexed by Google with special apostrophes (url encoded as '%E2%80%99'). We corrected the urls on the server, but Google is still pointing there and we didn't want to interrupt any SEO mojo here. Any thoughts why this won't work?
Current rewrite rule in .htaccess file:
# remove apostrophes from a string
RewriteRule ^(.*)’(.*)$ /$1$2 [L,R=301]
RewriteRule ^(.*)%E2%80%99(.*)$ /$1$2 [L,R=301]
Example replace this URL:
http://example.com/santa%E2%80%99s-comin-to-town/
with this URL:
http://example.com/santas-comin-to-town/
Try using this:
RewriteRule ^(.*)’(.*)$ /$1$2 [B,L,R=301]
RewriteRule ^(.*)([^\w].+\d)(.*)$ /$1$3 [B,L,R=301]
using the % character can have adverse effects on rewrite rules:
(%..%..%..) or (\%..\%..\%..)
should also work, although make sure you provide the [B] flag on the end of the rule.
more info
Use this rule for using hex code in rewrite rules:
RewriteRule ^(.*)\xE2\x80\x99(.*)$ /$1$2 [L,R=301]

Rewrite rule in htaccess for nice links and I need advice

I'm trying to make nice links in my apps.
I decided to rewrite links that look like these:
1. http://example.cz/get1/get2/get3
2. http://example.cz
Into these (I have php appliactions only):
1. http://example.cz/index.php?path=get1+get2+get3
2. http://example.cz/index.php?path=
I'm removing www before links.
I keep failing to rewrite it into .htaccess.
I'm also looking for advice if the primary idea of rewriting get params into path=get1+get2+get3 is good? Right now I can see that link like this http://www.example.cz/you+me/ could possibly fail somewhere. Do you have any better solution?
So question is: How to rewrite it into .htaccess and how to solve possible problems with link that contains '+'
EDIT:
I improved my skills a little and I did this:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.php?path=/$1 [R=301,L] # 301 is here so I can see how does it work
# everything above works well (as I want)
# link now look like this one:
# http://example.net/index.php?path=/get1/get2/get3
# Now I was looking for universal rule that will rewrite my get params...
# First I did this:
RewriteCond %{REQUEST_URI} /([^/]+)
RewriteCond %1 !index.php(.*)
RewriteRule /([^/]+) $1+ [R=301,L]
# If any part of request uri is string that matches pattern /([^/]+)
# And if ([^/]+) doesn't match index.php(.*)
# then rewrite every /([^/]+) into $1+
# Now I see that it is incorrect, but I wasn't able to fix it
# So then I did different rule
RewriteRule ^([^=]+=[^\/]*)\/([^\/]+)(.*)$ $1$2+$3 [R=301,L]
# start of string
# first var is: one or more chars except =, =, zero or more chars except /
# /
# second var is: one or more chars except /
# third var is: zero or more chars
# end of string
I think second idea was much more better, but it doesn't work too. Please help me to fix it.
You can do this with the Apache module mod_rewrite. Chances are you probably already have it installed. Try this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?path=$1+$2+$3 [L]
This regex assumes the URL will always contain three groups of text between slashes. You can tweak it as needed.
Also note that Apache never sees the URL hash, so you won't be able to match it in a rewrite rule. Luckily, it looks like you don't want to do anything with it anyway. Just use the rule above, and the hash will remain at the end of the URL in the browser.
I did solution. Problem was that after adding index.php?path= I wasn't able to work with query string...
The final universal solution that turn links from http://www.example.net/get1/get2/get3 to
http://example.net/index.php?path=get1+get2+get3:
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.phppath=/$1 [R=301,L]
RewriteRule ^([^=]+=[^/]*)/([^/]+)(.*)$ $1$2+/$3 [R=301,L]
RewriteRule ^(.*)\+/+$ $1 [R=301,L]
RewriteRule ^(.*)path=(.*)$ $1?path=$2 [R=301,L]