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!
Related
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]
I'm redirecting a http www. URL to a new non-www. domain at https, but one particular URL pattern has changed in structure, so I'd like to use a ReWrite Rule for it in htaccess.
Old:
http://www.example.com/travel.php?cat=cat1&id=1&title=title-string
http://www.example.com/travel/cat1/1/title-string
New:
https://newdomain.com/travel/cat1-title-string
So for the redirect, the "id" parameter disappears completely, but I'd like the "cat" parameter and hyphen to prefix the "title" parameter in the new URL.
I think this is possible, but I can't figure out how? If it isn't, I'll settle for a rule without the cat parameter:
https://newdomain.com/travel/title-string
I would like to use an or expression to match the "cat":
(cat1|cat2|cat3)
For the first kind of URL you do this:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^cat=([^&]*)&id=[^&]*&title=(.*)?$ [NC]
RewriteRule ^travel\.php$ https://newdomain.com/travel/%1-%2? [R=301,L]
For the second URL you do this:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^\/travel\/([^\/]*)\/[^\/]*\/(.*)?$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/travel/%1-%2 [R=301,L]
If cat1 is should only be work for some spezial values we have to redefine this:
For the first kind of URL you do this:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^cat=(cat1|cat2|cat3)&id=[^&]*&title=(.*)?$ [NC]
RewriteRule ^travel\.php$ https://newdomain.com/travel/%1-%2? [R=301,L]
For the second URL you do this:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^\/travel\/(cat1|cat2|cat3)\/[^\/]*\/(.*)?$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/travel/%1-%2 [R=301,L]
Replace cat1|cat2|cat3 with your values
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.hemsida\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.hemsida\.eu$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ "https\:\/\/testar\.se\/sv/$1" [R=301,L]
So this one will redirect all traffic from test.hemsida.eu to https://testar.se/sv/$1
But the problem is if test.hemsida.eu contains sv in the path, it redirects: https://testar.se/sv/sv/test
How can I get rid of one the sv, but still maintain it, if it isn't present in the URL?
You can use it like this by tweaking your regex:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?test\.hemsida\.eu$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ [NC]
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ [NC]
RewriteRule ^(?:sv/)?(.*)$ https://testar.se/sv/$1 [NC,NE,R=301,L]
(?:sv/)? (optional match) at the start of pattern will make sure that you always capture whatever comes after /sv/ or if there no /sv/ then complete match.
I need a hand with creating a regex rewrite rule for my .htaccess.
Here's the problem. My previous URLs structure was:
http://example.com/whatever-the-url-is/?lang=en
now I turned it into
http://example.com/en/whatever-the-url-is/
I'd like to create an .htaccess URL that 301 redirects all the URLs from the previous structure to the new one.
Also the .htaccess is shared between different domains, and so there should be
RewriteCond %{http_host} !^example.com$ [NC] condition at the beginning...
Is it possible? Do you have any suggestions?
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(.+?)(?:&|$) [NC]
RewriteRule ^(.*)$ /%1/$1? [R=301,L]
You can add the RewriteRule after RewriteCond %{HTTP_HOST}:
RewriteCond %{REQUEST_URI} !^/(?:en|fr|sp)/ [NC]
Where you test if langcode is already in the url.
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.