RewriteCond variable back-referencing of %2 - mod-rewrite

this is htaccess file on the server
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain.com(.*)$
RewriteRule ^(.*)$ http://domain.com/test\.php?user=%1&path=%2 [R]
According to my understanding of the above code if i request asher.domain.com/user it should rewrite to http://domain.com/test.php?user=asher&path=/user right?
Instead i get http://domain.com/test.php?user=asher&path= The %2 is empty. but if i use $1 instead of %2 i seem to get the right result.
I might be doing the silliest mistake ever but I am not sure where I my mistake is. Help me out here guys? where is the mistake in the rewrite rules that %2 is not working for me?

Using the $ syntax gives you RewriteRule backreferences, while the % will give you RewriteCond backreferences. The mod_rewrite documentation covers this.
In your case, your RewriteRule should look like:
RewriteRule ^(.*)$ http://domain.com/test\.php?user=%1&path=$1 [R]
Because you want the first group match from the previous RewriteCond and the first group match from the current RewriteRule.

Related

Relative path and mod_rewrite issue

Have a trouble with mod_rewrite.
I want :
htp://example.com/a/some_text -> htp://example.com/?p1=some_text and
htp://example.com/b/some_text -> htp://example.com/?p1=some_text
So, I type:
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^(.*)$ ?fsearch=$2 [QSA]
and get wrong relative paths in css, such as htp://example.com/a/CSS/main.css instead of htp://example.com/CSS/main.css. And get nothing in $2 too.
Help, please
$2 tries to pick the second capture group of the RewriteRules subject, but there is only one! You probably mean %2 which picks from the RewriteCond...
Since you only append a query string in your RewriteRule the original URI (path) is left untouched.
You can simplify the pattern in the RewriteRule since you are not interested in the subject anyway.
This is probably what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /?fsearch=%3 [QSA]
Much better however would be to rewrite directly to whatever script you actually want to process the request to safe another rewriting round to find the index document, so something like:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%3 [QSA]
And unless you decide to use the first capture group in the RewriteCond you can also simplify that further:
RewriteEngine on
RewriteCond %{REQUEST_URI} \/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%2 [QSA]

Rewrite rule can it be done?

Does anyone know how to rewrite something like
/index.php?option=com_k2&view=item&id=30:name-of-item
to
/index.php/name-of-item
or maybe
/index.php/blog/name-of-item
You can do that using a combination of RewriteCond and RewriteRule:
RewriteCond %{QUERY_STRING} ^.*:(.*)$
RewriteRule ^/index.php /index.php/%1
where %1 is the backreference to the group captured in the RewriteCond expression.
Sources:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond
http://wiki.apache.org/httpd/RewriteQueryString

RewriteCond Simple Issue

I need the URl http://mydomain.com/careers to go to http://mydomain.com/#!/careers
For what it's worth I've tried numerours variations around this with no success
RewriteCond %{REQUEST_URI} !^/careers$
RewriteRule (.*) /#!/careers [QSA,L]
Can anyone help?
"!" in your RewriteCond line means "not"...
Also, "[QSA,L]" means:
L means this is last rule (processing terminates after matching this one) and
QSA means query string append
But, becase R flag was not specified, this is done by sub-request and not a redirect, so the actual URL in your browser does not change...
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/careers$
RewriteRule (.*) /#!/careers [R,L,QSA]
Hope this helps

mod_rewrite: hierarchical sub-domains into hierarchical sub-directories

Consider the following problem, please. I have got domain.tld with hierarchical sub-domains like the following:
a.domain.tld
b.a.domain.tld
c.b.a.domain.tld
... etc.
There is also hypothetical directory structure in the web-root:
/foo
/a.
/a./b.
/a./b./bar
/a./b./c.
... etc.
I would like to achieve such rewrite that would cause Apache to serve directories in a way you see below.
a.domain.tld --> /a.
b.a.domain.tld --> /a./b.
c.b.a.domain.tld --> /a./b./c.
... etc.
Directories without trailing dot character would behave as normal sub-directories.
domain.tld/foo/ --> /foo
a.b.domain.tld/bar --> /a./b./bar
I can not use mod_vhost_alias and would be glad if the solution was achievable using mod_rewrite only. Is it possible to achieve such rewrite?
Thank's for all your advices.
--
nkd
Possible solution for 4 levels of sub-domains:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.tld
RewriteRule (.*) %1./$1 [R,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %2./%1./$1 [R,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %3./%2./%1./$1 [R,L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %4./%3./%2./%1./$1 [R,L]
Thank you.
--
nkd
The previous solution ends in very funny infinite redirect loop. Here's a solution I got now (not very elegant, but it works; but with a huge 'but'):
# Working solution for five levels of sub-domains
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC]
RewriteRule (.*) http://DOMAIN.TLD/%5./%4./%3./%2./%1./$1 [R,L]
Can somebody explain to me why (the hell) it works? It really does work, I tested it extensively. But why does it work actually? If I look at the RewriteRule line I doubt it should work... Thank you for your explanations.
BTW: If the above five rewrite conditions and rule work, it looks like it could be re-written in some sort of 'two-liner' (one condition plus one rule). I tried that already, by using the above rule and the following condition instead of the five conditions given above:
RewriteCond %{HTTP_HOST} ^(([^\.]+)\.)+DOMAIN\.TLD [NC]
and played with it a little but with no real success. Thanks for all ideas how to simplify
the rewrite stuff and make it more 'sane' (if possible).
--
nkd
It is possible if you send the part to be reversed to an external script via RewriteMap and have that handle it.

help rewriting this url. simple but not working

RewriteCond %{REQUEST_URI} !^/?ping.php
RewriteRule ^/\?(.*)$ ping.php?url=$1 [L]
i am trying to match any character that follows /?
www.mysite.com/?someurl.com
instead it keeps loading the index.html !
You cannot match the query string with mod_rewrite, but if you still want to pass it on, you can add %{QUERY_STRING} to the resultant url, for example
RewriteRule ^.*$ ping.php?url=%{QUERY_STRING} [L]

Resources