Redirect every page no matter how deeply nested to a single page on same domain - apache

I've seen several answers to this, but none of them seem to work on subdirectories or nested URLs.
Basically, I want to redirect everything on a domain, no matter whether specified with a direct URL or a directory, no matter how deeply nested, to a "site closed" or "maintenance" page, on the same domain.
For example, all of these:
example.com
example.com/page.html
example.com/sub
example.com/sub/page.html
example.com/sub/sub2
example.com/sub/sub2/page.html
...should redirect to https://example.com/index_closed.html
I can get it to work on example.com, but not nested directories.
Also, it should NOT redirect .css files, if possible. Thanks!

RewriteBase /
RewriteRule ^$ https://example.com/index_closed.html [R=301,L]

I found that the following works:
RewriteCond %{REQUEST_URI} !/index_closed.html$
RewriteCond %{REQUEST_URI} !.css$ [NC]
RewriteCond %{REQUEST_URI} !.jpg$ [NC]
RewriteRule $ /index_closed.html [R=302,L]

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

.htaccess redirect to subdomain url with hash

I'm trying to redirect a site with the following url structure:
https://example.com/2021/about
to
https://2021.example.com/#about
https://example.com/2021/visit
to
https://2021.example.com/#visit
how can i do this?
i tried adding this to the /about directory in the original domain:
RewriteEngine on
RewriteBase /
RewriteRule (.*) https://2021.example.com/#about [R=301,NE, L]
but what i got after the redirect was
https://2021.example.com/#about2021/about
which is not right. any help is appreciated
[EDIT] i only want to apply this to some folders, like /2021/about and 2021/visit
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^(20\d{2})/(.+?)/?$ https://$1.%1/#$2 [R=301,L,NE]
Make sure this is your topmost rule in .htaccess and you clear your browser cache before testing this new rule.
Could you please try following, written based on your shown samples. Please make sure to clear your browser cache before testing your URLs. Following will only be applied to 2021/about OR 2021/visit uris.
RewriteEngine ON
RewriteCond %{HTTP_HOST} (example\.com) [NC]
RewriteRule ^(2021)/(about|visit)/?$ http://$1.%1/#$2 [R=301,NC,NE,L]

.htaccess redirect to another domain except admin page

I am asking because I can not for the live of me figure out what is wrong and so far none of the StackOverflow answers worked.
I have to redirect a domain to another subdomain, except the admin. For example:
sub1.domain.com/testsite/ shoud redirect to "sub2.domain.com/testsite/",
but sub1.domain.com/admin/ or "sub1.domain.com/de/admin/" should stay right where it is.
As a first step I tried to only check for the "admin", so everything would be redirected except "sub1.domain.com/admin/":
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteCond %{REQUEST_URI} !^\/admin
RewriteRule ^/?(.*)$ http\:\/\/sub2\.domain\.com%{REQUEST_URI} [R=301,L]
This one looked most promising, but it is not working. The second condition is not working and the admin page still gets redirected.
If anyone can help I would appreciate it.
EDIT:
I should have said that its a multi-domain site, which means we have a .htaccess file for all sites and that is the reason I specifically check for the domain.
I'm just posting this, but I can't test it!
But I guess this redirects EVERYTHING except that one domain.
RewriteCond %{HTTP_HOST} !^sub1.domain.com/admin/ [NC]
RewriteRule ^/(.*)$http\:\/\/sub2\.domain\.com%{REQUEST_URI} [R=301,L,NC]
I hope it works!
So,
I just found the reason (besides my stupidity). The site I should redirect was a Drupal Site. Thats why all links end up at the same location:
sub1.domain.com/index.php
The reason why my above Rewrite Condition was not working is, that sub1.domain.com/admin is being redirected to sub1.domain.com/index.php, which consequently ends up at: "sub2.domain.com/index.php". The correct rewrite rule looks like that:
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com
RewriteCond %{REQUEST_URI} !^/(admin|index\.php|de\/admin|it\/admin|user|de\/user|it\/user)
RewriteRule (.*) http://sub2.domain.com%{REQUEST_URI} [R=301,L]
This redirects everything except:
sub1.domain.com/admin
sub1.domain.com/de/admin
sub1.domain.com/it/admin
sub1.domain.com/user
sub1.domain.com/de/user
sub1.domain.com/it/user
and of course
sub1.domain.com/index.php
Since the last one also should not be redirected if the user types it in directly, it is not a perfect solution, but I can live with it.
RewriteCond is use to check condition weather to execute .htacess or not
For your case the solution may be as below:-
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(admin)$ http://sub1.domain.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://sub2.domain.com/$1 [R=301,L]

htaccess redirect subdirectory to subdomain pointing to the same location

Yeah .. yet another url rewrite question. Sorry but the solutions I tried didn't work and gave me infinite redirect loops :(
So, we have a directory on
example.com/interface/...
and want to redirect it to
service.example.com/interface/... (including all folders and files in it)
its physically the same location but example.com will move to another server soon and then interface.example.com will point to the old(current) server
Right now example.com and service.example.com point to the same web home directory (yay for duplicate content huh)
Also .. we have a rule in place
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
and I suspect this one is somehow, together with the other rules I tried, causing the infinite loop error because www.service.example.com does not exist (and shouldnt)
I suppose its rather simple but I just can't get my head around rewrite stuff :(
You can add a new rule for this:
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^interface(/.*)?$ http://service.example.com%{REQUEST_URI} [L,R=301,NC]

htaccess redirect all requests from subfolder to subdomain

I have the following (WordPress) URL canonical structure:
http://mysite.com/2011/10/my-post-name/
I want to send all requests from http://mysite.com/2011/... down to the same location on a subdomain.
So specifically that should be:
http://mysite.com/2011/10/my-post-name/ ---> http://subdomain.mysite.com/2011/10/my-post-name/
I'm not sure what the correct syntax is for this, does anyone know?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule (.*) http://subdomain.mysite.com$1 [R=301]
If you also need to match the first part of the path, then use
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/2011/(.*) http://subdomain.mysite.com/2011/$1 [R=301]
You should read the mod_rewrite documentation. It's confusing the first time, but after the 25th time or so it will make pretty good sense and serve you well.