Why my regular expression doesn't match this? - regex

Please help me with this problem. This is my htaccess rule :
RewriteCond %{REQUEST_METHOD} ^(GET)
RewriteRule ^ws/([^/]+)/([^/]+)/([A-Za-z]+)(&.+)?$ index.php?module=ws&wsType=$1&wsRessource=$2&wsAction=$3$4 [L,QSA]
This is what I want to match
ws/rest/user/get&criteria%5Befezf%5D=%2Ftot%2F&output=dump&compress=&session_id=cb932jrjakosubljl16loecbl1&api_key=a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
The problem is around %2Ftot%2F. If I remove %2F (which is a slash / ), rewrite rule works

Add the [B] flag, i.e. [L,QSA,B], or add
AllowEncodedSlashes On
to Apache, or use %252F instead of %2F.

Related

mod_rewrite RewriteRule anomaly

I'm preparing rewrite rules for my site. And I'm faced problem when target URL contains characters like "%3A".
Apache mod_rewrite just removes "%3" when rewrites url.
For example I need rewrite url
/primed-white-mdf-skirting+architrave/
to
/Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=%3AtopSellers%3AColour%3AWhite&text=#
I have generated rule for this. Here it is:
RewriteRule ^primed-white-mdf-skirting\+architrave/ /Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=%3AtopSellers%3AColour%3AWhite&text=# [R=301,L,NE]
So rewrite pass to:
/Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=AtopSellersAColourAWhite&text=%23
Why this happens? Please help
You need to escape the % otherwise %3 is considered a back-reference of captured group from RewriteCond:
RewriteRule ^primed-white-mdf-skirting\+architrave/ /Products/Decorating+Interiors/Mouldings/Skirting/c/1000589?q=\%3AtopSellers\%3AColour\%3AWhite&text=# [R=301,L,NE,QSA,NC]

Redirect using mod_rewrite

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^haveg/employer/([0-9]+)/(.*) haveg/employer.php?msg_id=$1
It works fine when i use
http://localhost/haveg/employer/7003/the-message-title
or
http://localhost/haveg/employer/7003/
The problem is here http://localhost/haveg/employer/7003 because i removed the forward slash at the end. it gives page not found error.
RewriteRule ^haveg/employer/([0-9]+)/?(.*) haveg/employer.php?msg_id=$1
I think adding a questionmark should allow it to match.
Try changing your last line to
RewriteRule ^haveg/employer/([0-9]+)([^0-9]*) haveg/employer.php?msg_id=$1
This should accept both cases.
I suggest you add another RewriteRule to make your intention clear. In this code the first rule handles the case when the URL ending with digits followed by an optional slash (when the msg_id query field is blank), and the second applies where there is a message following the digits.
RewriteRule ^haveg/employer/([0-9]+)/?$ haveg/employer.php?msg_id=
RewriteRule ^haveg/employer/([0-9]+)/([^/]+)$ haveg/employer.php?msg_id=$1 [L]

rewrite multiple parameters .htaccess

i need to rewrite www.sample.com/user/john/ to www.sample.com/user/?u=john
this code works only if i leave out the trailing / "www.sample.com/user/john" how do i get it to work like "www.sample.com/user/john/" as well?
RewriteRule ^([A-Za-z0-9_]+)$ index.cfm?u=$1 [L]
now i would need it to function a step further
www.sample.com/user/john/23564/ to rewrite like "www.sample.com/user/?u=john&i=23564"
one thing "www.sample.com/user/john" should still word even w/o the/23564/
thanks
Add the optional slash (and the "user/" part of the URL):
RewriteRule ^user/([A-Za-z0-9_]+)/?$ index.cfm?u=$1 [L]
And the next step:
RewriteRule ^user/([A-Za-z0-9_]+)(/([0-9]+))?/?$ index.cfm?u=$1&i=$3 [L]

Regex pattern help (I almost have it, just need a bit of expertise to finish it)

I need to match two cases
js/example_directory/example_name.js
and
js/example_directory/example_name.js?12345
(where 12345 is a digit string of unknown length and the directory can be limitless in depth or not exist at all)
I need to capture in both cases everything between js/ and .js
and if ? exists capture the digit string after ?
This is what I have so far
^js/(.*).js\??(\d+)?
This works except it also captures
js/example_directory/example_name.js12345
I want the regex to ignore that. Any suggestions?
Thank you all!
Test your patterns here
Answer:
Using Gumbo's information my final rewrite rule is as follows.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^\d*$
RewriteRule ^js/(.*)\.js$ js.php?f=$1.js&v=%0 [L]
</IfModule>
Include the whole querystring pattern, including the ? in one conditional match.
^js/(.*).js(\?\d+)?
mod_rewrite’s RewriteRule directive does only test the URI path and not the query. So using a rule like the following does already match both URIs:
RewriteRule ^js/(.*)\.js$ …
If you now want to test the query too, you need to use an additional RewriteCond:
RewriteCond %{QUERY_STRING} ^\d*$
RewriteRule ^js/(.*)\.js$ …
The match of the last successful RewriteCond can be referred to with %n, so in case of the whole match %0 or in this case even just %{QUERY_STRING} directly.
As far as regular expressions go - you can use the (?:) (non capture grouping) to make the \?(\d+) as a chunck, optional like so:
^js/(.*).js(?:\?(\d+))?
You really don't >need< to use the ?: (non capture) portion, but if you don't, back references will be changed - 1 will point at the filename, 2 will point at ?1234 and 3 will be 1234

Can mod_rewrite preserve a double slash?

Im just learning mod_rewrite and regex stuff, and what I'm trying to do is pass variables of any name, with any number of variables and values, into a script and have them forwarded to a different script.
here is what I have so far:
RewriteEngine on
RewriteRule ^script\$(.*[\])? anotherscript?ip=%{REMOTE_ADDR}&$1 [L]
That all seems to work except that one of the parameters I'm passing is a URL and the // after http:// always gets stripped down to one slash.
for example, I do
script$url=http://www.stackoverflow.com
then it redirects to:
anotherscript?ip=127.0.0.1&url=http:/www.stackoverflow.com
and the second script chokes on the single-slash.
I realize that preserving a double-slash is the exact opposite of what people usually do with mod_rewrite. Is there a way I can preserve the double-slash?
EDIT: Solution found with Gumbo's help.
RewriteCond %{THE_REQUEST} ^GET\ (.*)/script\$([^\s]+)
RewriteRule ^script\$(.*) anotherscript?ip=%{REMOTE_ADDR}&%2 [L]
I had to add that (.*) in front of /script on the RewriteCond, once I did that it got rid of the 404 errors and then it was just a matter of passing the matches through.
Try this rule:
RewriteCond %{THE_REQUEST} ^GET\ /script\$([^\s]+)
RewriteRule ^script\$.+ anotherscript?ip=%{REMOTE_ADDR}&%1 [L]
See Diggbar modrewrite- How do they pass URLs through modrewrite? for the explanation.
I Think there may be something wrong with the first part of your RewriteRule regex
^script\$(.*[\])?
The backslash ( \ ) is used to escape a special character into a litteral one, thus you are actually trying to match a closing bracket ( ] ), is that intended ?
try this
RewriteRule ^script\$(.*)? anotherscript?ip=%{REMOTE_ADDR}&$1 [L]