.htaccess redirect rule only root-domain [duplicate] - .htaccess

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).

Related

Exclude addon domain from rewrite rule?

I am trying to redirect all files in the folder mydomain.com/es to mydomain.com/
This works using
RedirectMatch 302 ^/es/ /index.php
However this is also redirecting the folder es/ in one of my addon domains.
So myaddondomain.com/es/ is going to mydomain.com/
How do I stop this? I have searched through the forum and found these rules that people have posted, but neither of them work
RewriteRule ^myaddondomain.com/?$ - [L]
or
RewriteCond %{REQUEST_URI} !^/myaddondomain.com/$
RedirectMatch 302 ^/es/ /index.php
If this should only apply to a particular domain (ie. hostname) then you need to use mod_rewrite (RewriteRule / RewriteCond) instead and check the HTTP_HOST in a condition. For example:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com [NC]
RewriteRule ^es/ /index.php [R,L]
Note that the URL-path matched by the RewriteRule pattern (1st argument) in a directory context does not start with a slash.
This also allows for an optional www subdomain. Remove (www\.)? in the CondPattern if that is not required.
Note that this redirects to /index.php, not strictly mydomain.com/ as you stated in your description. Maybe you should be redirecting to / instead?
Aside: As far as SEO is concerned, many-to-one redirects to the homepage will likely be seen as a soft-404.
Aside:
I have searched through the forum and found these rules that people have posted, but neither of them work
RewriteRule ^myaddondomain.com/?$ - [L]
or
RewriteCond %{REQUEST_URI} !^/myaddondomain.com/$
I would be curious where you saw these (on StackOverflow?) as they are obviously incorrect in this context.
The RewriteRule pattern (first rule) matches against the URL-path only, so the first rule will never match.
Again, the REQUEST_URI server variable (second rule) contains the URL-path only so the "negated" condition will always be successful.

How to rewrite URLs that cause duplicate content?

I have a lot of links with a double URL structure. Basically, I need a .htaccess rule that redirects all URLs that start /de (for German language) to the same URL with only a /.
example.com/de/Shop/Tradition/Jagd-Forst/
to
example.com/Shop/Tradition/Jagd-Forst/
Your rewrite rule should match starting with /de/ (with an optional strating slash, capture everything afterwards with (.*) and redirect to the capture group ($1) with a 301 permanent redirect:
RewriteEngine on
RewriteRule ^/?de/(.*) /$1 [R=301,L]

How to redirect url via .htaccess

I want to redirect via .htaccess
https://www.example.co/en/brand/Abc to https://www.example.co/en/brand/abc
I have tried
RewriteRule ^https://www.example.co/en/brand/Abc https://www.example.co/en/brand/abc [R=301,L]
The RewriteRule pattern (1st argument to the RewriteRule directive) matches against the path-part of the URL only, ie. /en/brand/Abc. An additional complication in per-directory .htaccess files is that the URL-path that is matched is also less the directory prefix (which always starts with a slash), so the URL-path does not start with a slash. In other words: en/brand/Abc (for an .htaccess file in the document root).
So, you will need to format the directive like this instead:
RewriteRule ^en/brand/Abc$ https://www.example.co/en/brand/abc [R=301,L]
(Assuming you already have RewriteEngine On defined and that this is near the top of your .htaccess file.)
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You may try something like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Don't want loops
RewriteCond %{REQUEST_URI} !/abc
RewriteCond %{REQUEST_URI} /Abc
RewriteRule . https://www.example.co/en/brand/abc [R=301,L]
URL are usually case-sensitive. Check this document, while domain names are not. Therefore "abc" and "Abc" are not the same and that's what the question is about. I think.

Redirect Each Page of Old Domain to Same Page URL on the New Domain

I know this question has been asked with all variations but I still cannot find the exact answer. I have changed my domain name. All website files and urls are still the same. old domain still points so the same server and I have the following code in htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.newdomain\.com\/$1" [R=301,L]
Problem is that this only redirects the old domain home page. What can I do so I don't have to add individual redirects for each page.
It sounds like you've put these directives in the wrong place in your .htaccess file. They need to go at the beginning of the file, not at the end.
These directives look typical of cPanel generated redirects. cPanel always places redirects at the end of the .htaccess file (which is often incorrect).
If you have a front-controller before this (such as that used by WordPress) then you will find that these directives will only get processed for requests to the document root (ie. the "homepage"), as all other requests will be stopped by the front-controller.
These directives can also be simplified:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]
I've removed the $ from the end of the CondPattern so as to match a FQDN (that ends in a dot).
There is also no need to backslash escape colons, slashes and dots in the RewriteRule substitution, as these characters carry no special meaning here (it's not a regex). (This unnecessary escaping is very typical of cPanel.)

.htaccess redirect all subfolders to specific path

I have multiple sites (subdirectories). I want to redirect all URLs of the format /(*)/login to /other/login.
I have tried:
RewriteRule ^/(.*)/login /other/login
#and
^(.*)/login /other/login
I've also tried Redirect.
Additionally, I notice that some subfolders have their own .htaccess files also. Do I need to put the redirect rule in the subfolder's .htaccess file? There surely must be a more efficient way.
I notice some subfolder have their own .htaccess files also, do I need to put the redirect rule in the subfolder .htaccess files?
Any mod_rewrite directives in child .htaccess files will, by default, completely override the mod_rewrite directives in any parent .htaccess file, they are not inherited. However, you can change this behaviour. On Apache 2.2 this is quite limited as you would need to change the child .htaccess file anyway, so it would probably be easier to simply duplicate this directive in the child config. But on Apache 2.4.8+ you can do all this in the parent .htaccess file. For example:
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} !^/other
RewriteRule ^[^/]+/login$ /other/login [R=302,L]
The InheritDownBefore option results in the current mod_rewrite directives being applied before the mod_rewrite directives in any child configs.
The RewriteCond directive is required in order to prevent a redirect loop (if the /other subdirectory does not have its own .htaccess file containing mod_rewrite directives).
This is an external "redirect" (as stated in your question). The URL changes in the browser's address bar. You can possibly change this to an internal rewrite (as your current directive implies) by removing the R flag (although this may depend on your application).
If you simply want a "redirect" then you could probably use a mod_alias RedirectMatch directive instead of the above mod_rewrite directives. This runs separately to mod_rewrite, but note that any mod_rewrite directives (in child configs) are processed first. For example:
RedirectMatch 302 ^/(?!other)[^/]+/login$ /other/login
The RedirectMatch directive uses a regex, whereas Redirect (also mod_alias) uses simple prefix matching. So, you couldn't match this specific pattern using a simply Redirect.
The (?!other) part is a negative lookahead (zero-width assertion) that checks that the URL-path being matched does not start other - in order to avoid a redirect loop.
Note also that RewriteRule does not use the slash prefix on the URL-path, whereas, RedirectMatch does. And there is no need to capture the URL-path (ie. by enclosing the regex in parentheses) if this is not required.
I have try several variants (you can see marked lines, that doesn't works),
my .htaccess:
#AddType application/x-httpd-php .html .htm
RewriteEngine on
RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]*)+^(.*)$ index.php?dir=$1&path=$2 [NC,L,QSA]
#RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]
#FallbackResource index.php
It works with all urls: /, /index, /index.html, /main/main.html.
Then echo var_dumps($_GET); gives:
array(1) { ["path"]=> string(10) "index.html" }
Now all requests goes into index.php.

Resources