RewriteCond - how to escape this? - apache

Hi am having a problem rewriting this url - how do I escape it so it will correct itself?
The url was sent out into the public domain like so:
http://www.domain.com/directory/directory-2/10018485&key=3945
I need to write a rule to correct it:
http://www.domain.com/directory/directory-2/10018485?key=3945
`
Am tring to do it like so, without success....
RewriteCond %{REQUEST_URI} \/directory\/directory-2\/10018485&key=3945
RewriteRule ^(.*)$ /directory/directory-2/?id=10018485&key=3945 [L]
Thanks :)

You don't need to escape the & in the match to %26. What you have works fine for me, not sure why it isn't working for you. But you don't need a RewriteCond, you can combine the 2 into a single rule. These work for me when the actual request is /directory/directory-2/10018485&key=3945 or directory/directory-2/10018485%26key=3945
RewriteRule ^directory/directory-2/10018485&key=3945$ /directory/directory-2/?id=10018485&key=3945 [L]
In a more abstract sense:
RewriteRule ^directory/directory-2/([0-9]+)&key=([0-9]+)$ /directory/directory-2/?id=$1&key=$2 [L]

Related

Rewrite Rule - need guidance

I need to do some redirecting to get some internal links to work but I'm having a complete block.
The url would be http://www.something.com/faqs/What_happens_if_I_move_home?
redirected to http://www.something.com/faqs/index/What_happens_if_I_move_home?
but it must look like the original url. I'm sure there is a simple answer but rewrite rules and regex are a mystery to me at times.
I did try RewriteRule ^faqs(/.*)?$ /faqs/index$1 [R,L,NC]
amongst many others!
try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/faqs/index
RewriteRule ^faqs/(.*) /faqs/index/$1 [L,NC]

Url rewriting mod_rewrite with and without query string

I'm trying to rewrite from domain.com/page/soft-15/android-26/ to page.php?cat=15&os=26 with this code:
RewriteRule ^page/([0-9]+)\-([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)$ page.php?cat=$1&os=$2
I thinks it works fine but, how can i rewrite domain.com/page/ and domain.com/page (without the last forward slash) to domain.com/page.php keeping both rules working?
ok think i got it mixing Jimp & Jon code
RewriteRule ^page/?$ page.php [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1&os=$2 [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1 [L]
This math with
domain.com/page
domain.com/page/
domain.com/page/soft-15
domain.com/page/soft-15/
domain.com/page/15
domain.com/page/15/
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/soft-15/26
domain.com/page/15/26
and so on...
Add a trailing /? to your pattern (before the $ anchor). The ? makes the / optional.
Additionally, your patterns seem backward, matching the digits before the characters. Try this:
RewriteRule ^page/(?:([a-zA-Z\d-]*)-)?(\d+)/(?:([a-zA-Z\d-]*)-)?(\d+)/?$ page.php?cat=$1&os=$2
That should match these variations like these:
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/15/android-26
domain.com/page/15/android-26/
domain.com/page/soft-15/26
domain.com/page/soft-15/26/
domain.com/page/15/26
domain.com/page/15/26/
Your backreferences don't seem to jive with the example that you're using:
/page/soft-15/android-26/
to page.php?cat=15&os=26
Your regex looks like it's matching:
/page/15-soft/26-android/
And rewriting to:
page.php?cat=15&os=soft
If you're going by your example, you'd want something like:
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1&os=$2
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1
RewriteRule ^page/?$ page.php

Getting URL parameters with mod_rewrite

Here is my current rewrite rule
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^post/([^/\.]+)/?$ index.php?post=$1 [L]
RewriteRule ^cat/([^/\.]+)/?$ index.php?cat=$1 [L]
three lines to do basically the same thing. Is there a way I can clean this up a bit? Is there one line that will accomplish this?
Also, Is there a way to pull multiple variables from a single url string? So if I had something like:
http://www.mysite.com?page=foo&id=123&color=red
how would I convert it to this:
http://www.mysite.com/page/foo/id/123/color/red
RewriteRule ^(cat|post|page)/([^/\.]+)/?$ index.php?$1=$2 [L]
because jacek_K to you one part, I give you answer of second part :
to rewrite this URL(http://www.mysite.com/page/foo/id/123/color/red), use this:
RewriteRule ^page\/([^/\.]+)\/id\/(\d+)\/color\/([^/\.]+) index.php?page=$1&id=$2&color=$3 [L]

Apache rewrite rule with parameters?

I have the following URL:
http://domain.com/index.php?m=feedback&cSubject=My Subject
I want to have a rewrite rule so that the following:
http://domain.com/feedback?Subject=My Subject
maps to the previous url. Heres my rule at the moment:
RewriteRule ^feedback?Subject=(.*)$ index.php?m=feedback&cSubject=$1
Doesn't seem to be working tho! Any ideas?
Query Strings are not parsed by Apache Mod_Rewrite, but there is a workaround. Try this
RewriteRule ^feedback/?$ index.php?m=feedback&c%{QUERY_STRING} [NC,L]
You can use RewriteCond statement to do exactly what you want:
RewriteEngine On
RewriteCond %{QUERY_STRING} Subject=(.*)
RewriteRule ^feedback$ index.php?m=feedback&cSubject=%1 [L]
There seems to be an = missing from clops answer to give..
RewriteRule ^feedback/?$ index.php?m=feedback&c=%{QUERY_STRING} [NC,L]
.. at least I need one to make it work.

mod_rewrite weird problem

I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom