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
Related
I would like to redirect via a RewriteRule (mod_rewrite) enabled in httpd.conf my URL:
https://mysite.domain.tld/index_php_file.php?ab=ident_keys&ac=5GU7VBNAH45DA5
TO:
https://mysite.domain.tld/index_php_file.php?ab=ident_key_1024&ac=5GU7VBNAH45DA5
I have tried it with a number of rules without luck:
RewriteCond %{HTTP_HOST} hmysite.domain.tld
RewriteRule ^/index_php_file\.php\?ab=ident_keys&ac=$ https://hmysite.domain.tld/index_php_file.php?ab=ident_key_1024&ac= [R=301,L,QSA]
nor
RewriteCond %{QUERY_STRING} ^ac=(.*)$
RewriteRule ^/?([a-z-0-9-_.]+)$ /$1/index_php_file.php?ab=ident_key_1024&ac=%1 [L,R=301]
seems to rewrite the URL.
Any suggestions on what I'm missing?
Thank you very much.
I found the solution, it may help someone.
RewriteCond %{QUERY_STRING} ac=([A-Z0-9]+)
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index_php_file.php?ab=ident_key_1024&ac=$1 [R=301,L]
It is enough to look for the string (containing UPPERCASE chars and numbers only) with the RewriteCond and rewrite the URL RewriteRule to the desired format and append the value of the variable from the query.
Do not forget to enable the mod_rewrite module within Apache. To take effect a restart is also necessary of course.
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]
given this url http://sub.domain.org/sub/other_string i need to have it rewritten to: http://newdomain/sub/...
i'm trying this rewrite, but i think i miss something in the RewriteCond.
in the second Cond can i use the capture of the first Cond?
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.org$
RewriteCond %{REQUEST_URI} ^/%1/.*$ [NC]
RewriteRule (.*) /%1/$1 [nosubreq,QSA,L]
This are my urls
http://test.aDomain.com/x-61bff
http://test.aDomain.com/x-ssmJhs54as65d4
http://test.aDomain.com/x-115545454
my rewrite rules
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
RewriteRule ^create-account create-account.php [L]
RewriteRule ^account account.php [L]
RewriteRule ^impress impress.php [L]
RewriteRule ^(x\-[a-zA-Z0-9]+) redirect.php?code=$1 [L]
The result is
http://test.aDomain.com/redirect.php
But why? it should be
http://test.aDomain.com/redirect.php?code=x-61bff
i dont get it ... can anybody help?
mainly because you got the regex wrong:
the / between the host-name and the path is really part of the path (e.g. it is /x-61bff instead of x-61bff). however, your regex matches only an x at the very beginning, thus /x-61bff will never match.
the minus-sign has only a special meaning within square brackets (e.g. [A-Z]); outside it is just another character, there is no need to escape it
so your rewrite-rule really should look like:
RewriteRule ^/(x-[a-zA-Z0-9]*) redirect.php?code=$1 [L]
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.