I need to redirect around 300 URLs on a multidomain site that has the same URL structure on the different domains. For example:
https://www.example.com/de/products.html needs to be redirected to https://www.example.org/de/products.html
So my usual approach does not work:
RedirectMatch 301 /de/products.html$ /de/products.html
I would need something like
RedirectMatch 301 https://www.example.com/de/products.html$ https://www.example.org/de/products.html
which obviously doesn't work or I just didn't get to work.
Not sure if important, but it's a TYPO3 instance.
The mod_alias RedirectMatch directive matches against the URL-path only. To match the hostname you'll need to use mod_rewrite with an additional condition (RewriteCond directive) that checks against the HTTP_HOST server variable (the value of the Host HTTP request header).
Also, since the URL structure is the same on both domains then you only need a single rule - just use the same URL-path from the initial request. No need to do one-by-one redirects as you seem to be trying to do.
For example, the following would need to go at the top of the .htaccess file before any existing rewrites:
RewriteEngine On
# Redirect everything from example.com to example.org and preserve the URL-path
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^ https://www.example.org%{REQUEST_URI} [R=301,L]
This checks for both example.com and www.example.com.
The REQUEST_URI server variable already contains a slash prefix, hence it is omitted in the substitution string.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
UPDATE:
But I don't want to redirect all URLs, just some.
To redirect a specific URL to the same URL at the target domain, as per your original example:
# Redirect "/de/product.html" only
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^de/products\.html$ https://www.example.org/$0 [R=301,L]
The above redirects https://www.example.com/de/products.html only to https://www.example.org/de/products.html.
The $0 backreference contains the entire URL-path that the RewriteRule pattern matches against.
How to extend your snippet with /de/ or /fr/ etc.? For example I want to redirect example.com/de/products.html but not example.com/products.html
Maybe the above example is what you require. Alternatively, to redirect /de/<something> (or /fr/<something>) only and not just /<something>, you could do something like this:
# Redirect "/<lang>/<something>" only, where <lang> is "de" or "fr"
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(de|fr)/[^/]+$ https://www.example.org/$0 [R=301,L]
The above will redirect https://example.com/de/<something> to https://www.example.org/de/<something>.
Related
My htaccess file currently redirects everything and has this
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [R,L]
I need to exclude two urls that begin with "send"
I changed the last line to
RewriteRule !^send(.*) https://www.domain.co.uk/$1 [R,L]
It excluded the send urls but any url in a subfolder is redirected to the root index page.
RewriteRule !^send(.*) https://www.domain.co.uk/$1 [R,L]
Negated patterns don't capture anything (by definition), but trying to capture everything after send when send is not present in the URL, doesn't make much sense.
You can do something like the following and use the REQUEST_URI server variable in the substitution instead of the backreference:
RewriteRule !^send https://www.domain.co.uk%{REQUEST_URI} [R,L]
Note that the REQUEST_URI server variable already contains the slash prefix.
So I'm trying to redirect traffic that goes to other pages using htaccess from a domain to a new subdomain but I'd like to keep traffic to the homepage there.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http\:\/\/newsubdomain\.domain\.com\/" [R=301,L]
This just redirects everything to the new subdomain. That's where I'd like some help, how can I keep the traffic that comes to the homepage on olddmain.com and redirect queries to olddomain/post and have them go to newsubdomain.domain.com/post
Thanks
This just redirects everything to the new subdomain.
No it doesn't. (If everything is being redirected then either something else is doing that or you are seeing a cached redirect.)
The directives as posted actually do the exact opposite of what you're trying to do. These directives only redirect the homepage (ie. requests for the document root).
To redirect everything, except the homepage, you can do something like the following at the top of the .htaccess file:
# Redirect everything except the homepage
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule !^$ https://newsubdomain.domain.com%{REQUEST_URI} [R=301,L]
The RewriteRule pattern !^$ matches everything except the root (an empty URL-path).
You will need to clear your browser cache before testing.
Test first with 302 (temporary) redirects to avoid potential caching issues.
Additional notes:
There is no need to backslash-escape colons, slashes and dots in the RewriteRule substitution string (2nd argument) - this is an "ordinary" string, not a regex. (This unnecessary escaping is quite typical of an attempt at using cPanel's "redirect" generation.)
I assume this should be HTTPS, not HTTP?
The two conditions (RewriteCond directives) can be combined into one.
I'm trying to redirect a route to a subdomain if it contains a certain string:
For example, if domain contains /foo/bar/:
http://example.com/foo/bar/123/456.jpg should be redirected to the subdomain http://sub.example.com/bar/123/456.jpg.
I mean, I would need to remove only /foo/ from the route and redirect the result to a subdomain.
I don't know how to remove a certain string in the middle of the route keeping following strings.
Try something like the following near the top of the .htaccess file in the root of example.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^foo/(bar/\d+/\d+\.jpg)$ http://sub.example.com/$1 [R,L]
If the subdomain points to a completely different area of the filesystem then you can remove the RewriteCond directive that checks the hostname. Or if the /foo/ prefix should always be removed (regardless of which host is requested).
This matches requests of the form /foo/bar/<number>/<number>.jpg.
$1 is a backreference to the captured group in the RewriteRule pattern that specifically omits the foo/ prefix.
This is a temporary (302) redirect.
if domain contains /foo/bar/
That is part of the URL-path, not the domain.
I'm trying to match a simple rule to rewrite a url but it's just not matching. I want to redirect
https://example.com/web/thanks/
to
https://example.com/thanks.php
Here's what I've tried
RewriteEngine On
RewriteBase /
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteBase /
RewriteRule ^web/thanks/$ https://example.com/thanks.php [R=302,L]
and many more tiny variations but none of them are triggering. I tried using this online tool and it returns "This rule was not met". What am I doing wrong?
To rewrite, just use your last rule
RewriteRule ^web/thanks/?$ /thanks.php [L]
with the following changes
no RewriteBase, this is only relevant for some relative URLs
optional trailing slash /?, if you want both /web/thanks or /web/thanks/ to work
no domain name, because this might trigger a redirect instead of a rewrite, see RewriteRule
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.
no R|redirect flag, because this triggers a redirect instead of a rewrite
The pattern ^.*thanks/$ or ^(.*)thanks/$ also works, but it matches any URL ending in thanks/, like /hellothanks/, /areyousurethanks/, /some/path/thanks/, ...
I have got url:
ipaddress/panelname/main/index.php
How to rebuild it to
ipaddress/center/index.php
?
ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.
I tryed to do this in .htaccess:
RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1
but i don't see redirect from panelname to center.
but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)
How to rewrite all to another links and howto see redirect from old links to my new? Thank you.
RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.
Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).
You either have to do the following:
RewriteRule ^panelname(.*)$ /center$1 [R,L]
or:
RedirectMatch 302 ^/panelname(.*)$ /center$1
Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.
If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).
RewriteEngine on
# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]
# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]