Apache rewrite including query string manipulation - regex

Need to rewrite the following:
/bob/dave/XXX/?id=passport
to
/lucy/bob/dave/?path=XXX&id=passport
XXX being whatever
Not had a chance test yet but will post as soon as I have.
Any pointers would be great.
Thanks in advance.
Update: This is what I have so far:
RewriteCond %{HTTP_HOST} "^bert.com$" [NC]
RewriteCond %{REQUEST_URI} "^/bob/dave/([A-Za-z0-9-]+)\?id=([A-Za-z0-9_-]+)$" [NC]
RewriteRule ^/(.*) https://lucy/bob/dave/?path=$1&id=$2 [L,R=301]
I suspect I will have to use a QSA for part of this.

Got there in the end - this seems to work:
RewriteCond %{HTTP_HOST} "^bert.com$" [NC]
RewriteCond %{QUERY_STRING} "^id=([A-Za-z0-9_-]+)$" [NC]
RewriteRule ^/bob/dave/([A-Za-z0-9-]+) https://lucy/bob/dave/?urlPath=$1&icid=%1 [L,R=301]

Related

Same RewriteCond and multiple RewriteRule

I've got in my application some review rules here's example:
RewriteCond %{HTTP_HOST} ^(.*)-street\.
RewriteRule ^(.*)/(.*)\.html$ index.php?street=%1&$1=$2 [L]
RewriteCond %{HTTP_HOST} ^(.*)-street\.
RewriteRule ^(.*)$ index.php?street=%1 [L]
I expect if I type: example-street.example.com/type/param.html that it use this rule:
RewriteRule ^(.*)/(.*)\.html$ index.php?street=%1&$1=$2 [L]
but unfortunately it use less specified one.
What should I add/change in my code to get expected result?
EDIT
I want to achive this result:
example-street.example.com/type/param.html -> index.php?street=example&type=param
example-street.example.com/whateverelse -> index.php?street=example
Check this, hope it will work fine.
Check .htaccess here
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/type/param.html$
RewriteCond %{HTTP_HOST} ([a-z]+)\-([a-z]+)\.example\.com$
RewriteRule ^([a-z]+)\/([a-z]+)\.html$ /index.php?%2=%1&$1=$2 [R=301,L,QSA]
RewriteCond %{REQUEST_URI} ^/[a-z]+$
RewriteCond %{HTTP_HOST} ([a-z]+)\-([a-z]+)\.example\.com$
RewriteRule ^.*$ /index.php?%2=%1 [R=301,L,QSA]

How to get first subdomain with RewriteCond htaccess?

Consider this URL:
www.sub1.sub2.sub3.subdomain.domain.com
The only thing that is a given is "domain.com". Now, I want to forward this UGLY url to: subdomain.domain.com. So somehow I have to get the subdomain.
I tried:
RewriteCond %{HTTP_HOST} ^(.+)/.([^.]+)/.domain.com$ [NC]
RewriteRule ^(.*)$ http://%2.domain.com/$1 [L,R=301]
Which won't work, as htaccess doesn't support look behind.
Any workaround for this problem?
You can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.+?\.([^.]+\.domain\.com)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

.htaccess RewriteRule keeping URL structure

My current rewrite rule:
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
The result above works great so I can format the URL like
domain.com/a/b/c
I would like to add in a domain switch as well so the results I want is
sub.domain.com/a/b/c when you access it using domain.com/a/b/c
Currently here is what I have tried
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?a=$1&v=$2&id=$3 [L]
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule (.*)$ http://sub.domain.com/ [R=301,L]
But the result of this is
http://sub.domain.com/a=a&v=b&id=c
and needs to be
http://sub.domain.com/a/b/c
Thanks for the help!!
Reverse the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule (.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)?$ index.php?a=$1&v=$2&id=$3 [L,QSA]
Make sure to test this after clearing your browser cache.

how to make mod_rewrite to redirect from subdomains to querystring?

I want to make mod rewrite as the following example :
from google.com.mydomain.com to mydomain.com/index.php?domain=google.com
I use like
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^([^\.]+)\.mydomain.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
but this redirect google.mydomain.com to mydomain.com/index.php?domain=google
I want rule to redirect google.com not google
thanks
It is due to the incorrect regex. Try this rule:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{QUERY_STRING} !domain=
RewriteCond %{HTTP_HOST} ^(.+?)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /index.php?domain=%1 [L,QSA]
In your rule you're using [^\.]+ which matched until a dot is found therefore it is matching google instead of google.com.

Regex for url-encoded string in .htaccess

I have to addresses for one site (different langs) and I need a redirect with saving query string. So I need to catch this
http://www.example1.kereell.com/wp-login.php?action=logout&redirect_to=http%253A%252F%252Fwww.example2.kereell.com%252Fafiliados-de-la-zona%252F&_wpnonce=66909cdca0
and redirect to
http://www.example2.kereell.com/wp-login.php?action=logout&redirect_to=http%253A%252F%252Fwww.example2.kereell.com%252Fafiliados-de-la-zona%252F&_wpnonce=66909cdca0
in .htaccess file it should be something like this
RewriteCond %{HTTP_HOST} ^.*example1\.kereell\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/wp-login.php?action=logout&redirect_to=http%3A%2F%2Fwww.example2.kereell.com%2Fafiliados-de-la-zona%2F&_wpnonce=66909cdca0$ [NC]
RewriteRule ^(.*)$ http://www.example2.kereell.com/$1 [R=301,L]
I tried a lot of everything (escaping "%", regex classes) but didn't get it to work because of urlencoded characters. If I replace all urlencoded symbols - it works..
So the solution was really easy as it suppose to be:
RewriteCond %{HTTP_HOST} ^.*example1\.kereell\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/wp-login.php$ [NC]
RewriteCond %{QUERY_STRING} ^action=logout&redirect_to=http[\%A-Z0-9]*www.example2.kereell.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example2.kereell.com/$1 [R=301,L]
Thanks for Qtax that paid my attention to QUERY_STRING!