Apache Rewrite Rule With % characters - apache

I have a url that needs to be rewritten.
http://aplus-carpetcleaning.com/home.php%3Fcity%3DGainesville%2520VA
I have tried this:
^home\.php%3Fcity%3DGainesville%2520VA$ "http://aplus-carpetcleaning.com/home.php?city=Gainevilles%20VA"
This is not being recognized. Tried various alternates but no dice.
Notes:
%3F is '?'
%3D is '=',
%2520 is who knows?

The rewriting module applies your regex patterns to decoded urls, not the urlencoded string you see inside the browsers url field.
So create your regex pattern using unescaped characters instead. Something like:
^home.php\?city=Gainesville&20VA$

Fixed it with:
RewriteRule ^home\.php(.*)city(.*)Gainesville(.*)VA$ "http:\/\/aplus\-carpetcleaning\.com\/home\.php?city=Gainesville VA" [R=301,L]

Related

.htaccess rule match part of new url and use as query string for old url

I am trying to match part of a URL and then use the matched expression to append onto the end of a query string from the old URL.
I have the following line in .htaccess, once I've worked out what I'm doing wrong I'll be able to fix the rest so for now I will just focus on the following line:
RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1
I would like ([^/]*)$ to appear where $1 is
So essentially: /league/29/matches/ would point to index.php?page=1074&league=29
Can anyone please tell me what I am doing wrong? :)
RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1
The $ (end-of-string anchor) after the subpattern ([^/]*)$, in the middle of the regex, does not make sense here and will cause the regex to fail. You should also be using the + quantifier here (1 or more). You are also missing the end-of-string anchor ($) at the end of the regex (otherwise the trailing /? is superfluous). Although you shouldn't really make the trailing slash optional on the rewrite as it potentially opens you up for duplicate content. (You should redirect to append/remove the trailing slash to canonicalise the URL instead.) You are also missing the L flag on the RewriteRule.
Try the following instead:
RewriteRule ^league/([^/]+)/matches/?$ index.php?page_id=1074&league=$1 [L]
Although, if you are expecting digits only (as in your example) then you should be matching digits, not anything. So, the following is perhaps "more" correct:
RewriteRule ^league/(\d+)/matches/$ index.php?page_id=1074&league=$1 [L]

Rewrite encoded URLs with RewriteRules

I was rewritting "domain.com/lolmeter/platformValue/usernameValue" (platformValue and usernameValue are values requested by the user with text inputs) with the following rewrite rule:
RewriteRule ^lolmeter/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)$ /lolmeter.html?platform=$1&username=$2 [L]
button.href = lolmeter/platformValue/usernameValue
I noticed that when the user inputs a whitespace or another non alphanumeric value, it is encoded with "%" symbols automatically, so I tried to rewrite the rule to accept them, like:
RewriteRule ^lolmeter/(([a-zA-Z0-9]|%)*)/(([a-zA-Z0-9]|%)*)$ /lolmeter.html?platform=$1&username=$2 [L]
But it doesn't work, I assume because of the parentheses. Which symbol should I use then for an inner "|" ?
P.S: Is there a more popular or modern way for changing URLs?
RewriteRule ^lolmeter/(([a-zA-Z0-9]|%)*)/(([a-zA-Z0-9]|%)*)$ /lolmeter.html?platform=$1&username=$2 [L]
The RewriteRule pattern matches against the %-decoded URL-path. So, if an encoded space (ie. %20) is present in the URL-path of the request then the rule matches against a literal space, not %20.
You can use the \s shorthand character class inside the character class in your regex to match any whitespace character.
For example:
RewriteRule ^lolmeter/([a-zA-Z0-9\s]+)/([a-zA-Z0-9\s]*)$ /lolmeter.html?platform=$1&username=$2 [L]
Note that I made the quantifier on the second/middle path segment + instead of * since I assume the middle path segment is not optional. Note that multiple contiguous slashes in the URL-path are also reduced before the regex is matched so if the middle path segment was omitted then the passed username would be seen as the platform, which I'm sure is not the intention.
Note also that in the above the space is not re-encoded in the resulting rewrite. Use the B flag to re-encode the space as a + in the query string. (If you specifically needed the space to be re-encoded as %20 then use the BNP flag as well - requires Apache 2.4.26)
P.S: Is there a more popular or modern way for changing URLs?
Not sure exactly what you mean by this, but mod_rewrite on Apache is the URL rewriting module. Always has been and probably always will be.
However, you don't necessarily need to rewrite the request the way you have done, although you may still want to match the URL in a similar way (depending on what else you are doing). You could perhaps just rewrite the request to lolmeter.html and have your script parse the URL-path directly, rather than the query string.
Or, I suppose the "modern way" would be to rewrite everything to a "front-controller" - an entry script that parses the URL and "routes" the request appropriately. This avoids having to have a multitude of rewrites in .htaccess. Although this isn't anything "new", it has perhaps become more common. Many CMS/frameworks use this pattern.

RewriteRule for Gmail Email Addresses

I'm trying to create a rewrite rule for unsubscribe URLs so that the url
https://example.com/unsubscribe/myemail#example.com/
will be re-written to
https://example.com/unsubscribe.php?email=myemail#example.com
In the past I've always used the following rule with no problems
ReWriteRule ^unsubscribe/(.*)/?$ /unsubscribe.php?email=$1 [NC,L]
However, when testing this recently, it seems to be replacing the "+" character (as is used commonly with gmail tagging, for example "myemail+spam#example.com") with an empty space, creating an email address different to the one entered by the user. This is a problem. You can see an example here:
Example Rewrite Rule Processing
I don't really get why this is happening as the "(.*)" filter should allow any character any number of times, shouldn't it?
Any suggestions would be greatly appreciated.
You can use the mod-rewrite B flag in your rule:
ReWriteRule ^unsubscribe/(.*)/?$ /unsubscribe.php?email=$1 [NC,L,B]
From the apache mod-rewrite flag manual :
The [B] flag instructs RewriteRule to escape non-alphanumeric characters before applying the transformation.
In 2.4.26 and later, you can limit the escaping to specific characters in backreferences by listing them: [B=#?;]. Note: The space character can be used in the list of characters to escape, but it cannot be the last character in the list.
mod_rewrite has to unescape URLs before mapping them, so backreferences are unescaped at the time they are applied .

rewrite spaces to redirect to underscores when following a specific domain url string and only

how to write a modrewrite that will replace a space %20 with an underscore that will look like this. http://cityinsider.com/b/ocean%20shores_wa/mikes-seafood-ocean-shores will redirect to http://cityinsider.com/b/ocean_shores_wa/mikes-seafood-ocean-shores . All the underscores are permanent, but sometimes there is a space between two and three word cities. so any spaces found after only this type and part of url: cityinsider.com/b/ need to be replaced . So this won't effect any other url strings that don't look like the above; for example it should not work on spaces that are immediately after domain root e.g. cityinsider.com/%20/appleWood and shoulnd't work where it doesn't follow the cityinsider.com/b/ such as cityinsider.com/c/%20
Try:
RewriteEngine On
RewriteRule ^([^/]*)\ ([^/]*)/(.*)$ /$1_$2/$3 [L,R=301]

mod_rewrite rule gives 404 errors

I am having problems with mod_rewrite which is throwing me the same error 404 is as follows:
RewriteRule ^music.mp3?id=(.*)$ music.php?id= [L]
i need url /music.mp3?id=1 and real url /music.php?id=1
any idea??
I think something is misunderstood in the path RewriteRule
You can't match against the query string (everything after the ? in the URL) in a RewriteRule. But you're not really matching against it anyways, it looks like you just need to appended to your target URI, so:
RewriteRule ^music\.mp3$ music.php [L]
Should be good enough. Any query string parameters (like ?id=1) will automatically get appended at the end.
In your expression (the engine rule), what you want to say is:
"When the url starts with music.mp3?id=, take whatever is after the = and change the URL to music.php?id= and put the part after id="
In regular expressions the . character has special meaning. If you want to say "the dot character", and not give it special meaning, you need to escape it, but putting a \ behind it, like this:
^music\.mp3?id=(.*)$
#Jon Lin already gave you the other part, which is about query strings.