I need to redirect subdomains like
^([a-z]{2}|ceb|haw|hmn|zh-CN|zh-TW).example.com/uk/(.*)$
for example:
de.example.com/uk/
ceb.example.com/uk/
redirect to http://$1.example.com/en/
for example:
de.example.com/en/
ceb.example.com/en/
how to change in .htaccess?
I try to use following but not works:
RewriteRule ^([a-z]{2}|ceb|haw|hmn|zh-CN|zh-TW)/uk http://$1.ts2.space/en [R=301,L]
The RewriteRule pattern matches against the URL-path only. To match the hostname, you need an additional condition (RewriteCond directive) and match against the HTTP_HOST server variable (ie. the value of the Host HTTP request header).
In your example, you are redirecting to the same host, so there is no need to use a backreference in the substitution and you can omit the scheme+hostname entirely.
For example, the following redirects de.example.com/uk/ to de.example.com/en/ as per your first example.
RewriteCond %{HTTP_HOST} ^([a-z]{2}|ceb|haw|hmn|zh-CN|zh-TW)\.example\.com
RewriteRule ^uk/$ /en/ [R=302,L]
NB: Don't forget to backslash-escape literal dots in the regex.
Reference:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
Related
I am not a programmer of .htaccess code, I read the other related posts but do not understand them. Non of them do what I need.
I have a Wordpress site that runs on http://example.com/main but want to redirect http://example.com to http://otherexample.com/page.php. Also need that http://example.com/anyfile not be redirected.
Assuming example.com and otherexample.com point to different places then try something like the following in the root .htaccess file at example.com:
RedirectMatch 302 ^/$ http://otherexample.com/page.php
The regex ^/$ matches / only, ie. the root directory.
^ asserts the start-of-string (ie. URL-path)
/ matches a literal slash
$ asserts the end-of-string
UPDATE: I found that secondary.com also is redirected.
If you have multiple domains pointing to the same place and you only want to redirect one of them then you'll need to use mod_rewrite to check the hostname first before redirecting.
For example, the following would need to go at the top of the .htaccess file before the WordPress front-controller:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^$ http://otherexample.com/page.php [R=302,L]
Note that the RewriteRule pattern matches against the URL-path less the slash prefix, hence the regex ^$ (different to the RedirectMatch directive), which matches an empty URL-path (ie. the root directory only).
We have URLs of the form:
www.dev-studio.co.uk.
www.dev-studio.co.uk./a-sample-image
With the help of .htaccess rules, I am trying to remove the trailing dot (co.uk.) in the end of the domain name but I'm failing.
This is the rule I'm trying:
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.dev-studio.co.uk/%3 [L,R=302,NE]
But the %3 which should capture the 3rd group is returning empty.
The goal is to simple redirect www.dev-studio.co.uk./a-sample-image to www.dev-studio.co.uk/a-sample-image
I have tried all the other questions over here but the solutions are not working for me.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.example.co.uk/%3 [L,R=302,NE]
The HTTP_HOST server variable contains the hostname only (ie. the value of the Host HTTP request header), it does not contain the URL-path, so the %3 backreference is always empty.
You need to either capture the URL-path from the RewriteRule pattern. For example:
RewriteRule (.*) http://www.example.co.uk/$1 [R=302,L]
Or, use the REQUEST_URI server variable (which contains the full URL-path, including slash prefix) instead:
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
This should ultimately be a 301 (permanent) redirect, once you have confirmed it works OK.
Note that since you are redirecting to a specific domain, do you need a CondPattern that matches any .co.uk hostname? You could be specific:
RewriteCond %{HTTP_HOST} =www.example.co.uk.
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
The = prefix on the CondPattern changes it to a lexicographical string comparison (not a regex), so no need to escape the dots.
If you wanted an entirely generic solution to remove the trailing . (FQDN) from any requested host then you could do something like:
RewriteCond %{HTTP_HOST} (.+)\.$
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
Although you might want to combine this with your canonical redirects (eg. non-www to www / HTTP to HTTPS?) to avoid multiple redirects - although they are probably unlikely to occur all at once anyway, so probably not an issue.
I want to redirect all
homepage.com/?start=
to
homepage.com
but not all other urls with ?start= such as
homepage.com/xxxx/?start=
i tried this:
RewriteCond %{QUERY_STRING} start [NC]
RewriteRule .* http://homepage.com? [R=301,L]
but sure, it redirect to home page all URLS with ?start
RewriteRule .* http://homepage.com? [R=301,L]
The RewriteRule pattern, (eg. .*) matches against the URL-path.
The regex .* matches everything, so that's why it redirects every URL-path. To match an empty URL-path (ie. just the home page) then restrict the regex to match an empty URL-path. eg. ^$.
RewriteCond %{QUERY_STRING} \bstart= [NC]
RewriteRule ^$ http://example.com/ [QSD,R=302,L]
Assuming you're on Apache 2.4 then use the QSD (Query String Discard) flag instead of appending an empty query string to the substitution string. Note that there should be a slash after the hostname portion of the URL.
Note that the regex start matched that string anywhere (eg. mystart123, starter=, etc.). The regex \bstart= matches just the query string parameter name as stated.
You will need to clear your browser cache before testing.
Test first with 302 (temporary) redirects to avoid caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
I have changed the website URL structure and I want to redirect my users.
Need to redirect from:
https://example.com/find?q=batman
To
https://example.com/search?query=batman
where "batman" is the search phrase.
The problem for me, is how to change
q to query after ?
Something like that, it does not work
RewriteRule ^find?q=[^/] /search?query=[^/] [R=302,L]
The RewriteRule pattern matches against the URL-path only - this notably excludes the query string.
Instead, you need to use a RewriteCond directive to check the QUERY_STRING server variable and capture the remainder of the query string after the q=.
For example:
RewriteCond %{QUERY_STRING} ^q=(.*)
RewriteRule ^find$ /search?query=%1 [R=302,L]
%1 is a backreference to the captured group in the last matched CondPattern. ie. (.*) - the value of the q URL parameter and remainder of the query string.
Change the 302 (temporary) redirect to 301 (permanent) only once you have confirmed that it works OK - to avoid caching issues.
I want to redirect requests to the URL:
example.com/downloads
to
download.example.com/downloads
How can I write a regex rule for this for an .htaccess file?
You don't actually need mod_rewrite to do this. Nor do you need a regex, unless you're matching a pattern instead of just a string.
Redirect ^/downloads http://download.example.com/downloads
If you need to match a pattern (i.e. redirect each subdirectory to a subdomain - sounds like a bad idea to me, but whatever) then you do need a regex, but you still don't need mod_rewrite.
RedirectMatch ^/([^/+])(/.+)? http://$1.example.com/$1$2
This matches a string immediately following the domain, i.e. the "downloads" part of http://example.com/downloads/somefile, then any trailing part of the request, and builds an URL to redirect to using those two pieces.
Place this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^downloads(/.*)?$ http://download.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L,NC]