Redirect all path except a few in htaccess - apache

Id like to use the .htaccess file on an apache servier redirect all paths on a given domain to a new location but keep a few specific URLs live.
Redirect 301 /(.*) www.example.com
# but not /foo
# but not /bar
# but not /baz

You cannot do it using Redirect directive. I suggest using mod_rewrite:
RewriteEngine On
RewriteCond %{THE_REQUEST} !/(?:foo|bar|baz)[?/\s] [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,NE,R=301]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Related

Subdomain and it's folder rewrite

Let's explain my issues. I have folder which contains subdomain files.
Folder which contains subdomain files is called "subdomain".
My domain is example.com.
My subdomain is sub.example.com.
Issue is that when I type https://sub.example.com it works but same content is loaded when I type https://example.com/subdomain or https://sub.example.com/subdomain
My question is, how to achieve that https://example.com/subdomain(.*) will redirect to https://sub.example.com(.*) and same with https://sub.example.com/subdomain(.*) that will redirect to https://sub.example.com(.*)
Current .htaccess configuration is as follows:
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule (.*) /subdomain/%1%{REQUEST_URI} [L]
You can use this redirect rule as your topmost rule inside your site root .htaccess or in subdomain/.htaccess:
RewriteEngine On
# use THE_REQUEST to detect presence of /subdomain/ in original URL
RewriteCond %{THE_REQUEST} \s/+subdomain/(\S*) [NC]
RewriteRule ^ https://sub.example.com/%1 [L,NE,R=301]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Redirect documentroot to another folder in conf apache or .htccess

Good Day
I want to rewrite a url when request a subdirectory
When the user request
172.0.0.1/url
i want to rewrite to make this url point to
172.0.0.1/url/url
or the documentroot is in /var/www/html/url/url make this point to
172.0.0.1/url
The trick here is to avoid creating a rewrite and a redirect loop. You can put this code in your root folder htaccess
RewriteEngine On
# Redirect /url/url/xxx to /url/xxx
RewriteCond %{THE_REQUEST} \s/(url)/url/(.*)\s [NC]
RewriteRule ^ /%1/%2 [R=301,L]
# Internally rewrite back /url/xxx to /url/url/xxx
RewriteRule ^(url)/((?!url/).*)$ /$1/$1/$2 [L]
You can use .htaccess to redirect /url to /url/url
Just Create .htaccess file at /var/www/html/ with following contains:
RewriteEngine on
RewriteRule ^/?url/(.*)$ /url/url/$1 [R,L]
This will Redirect all incoming requests at /url and redirect them to /url/url
But you cannot redirect requests coming at /url/url to /url Because we are already redirecting request at /url, this will result in catch 22 and browser will keep redirecting back and forth between /url and /url/url.

htaccess redirection to one file for one domain and another file for another alias domain

I have 2 domains going to a vhost
example.com thisismyexample.com << this is an alias of the first one. It has got his own vhosts which is a symbolic link of the first one.
I need different behavior for each one so I'd like to redirect as:
if request comes for example.com -> example.com/index.php
if request comes for thisismyexample.com -> thisismyexample.com/admin.php
Both files index.php and admin.php are on the same directory which is my document root. At the moment there's a REQUEST_FILENAME rule and everything goes to index.php
I would try with redirectmatch but would like some opinions first
RedirectMatch ^example.com/$ http://example.com/index.php
RedirectMatch ^thisismyexample.com/$ http://thisismyexample.com/admin.php
Is this possible using htacces? If so, how would it be?
Use a RewriteRule with a RewriteCond that checks the host-header:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^ http://%{HTTP_HOST}/index.php [R,L]
RewriteCond %{HTTP_HOST} ^thisismyexample.com$
RewriteRule ^ http://%{HTTP_HOST}/admin.php [R,L]

301 redirect for all get request

I need to redirect all incoming requests get. for example: site.com?anystr to site.com
I tried to do so
RedirectMatch /?(.*)$ site.com
But it causes cyclic forwarding and i get browser error
RedirectMatch does match anything behind the domain name and before the query string. The regex /?(.*)$ will match any request.
You will need to use mod_rewrite. Enable mod_rewrite in the main config file of Apache and restart Apache. Then add the following to your .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} .+
RewriteRule ^ - [QSD,R,L]
Change the [R] flag to [R=301] after testing this works as expected.
I believe this is hat you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} .+
RewriteRule ^ /? [L,R=302]
i.e. any URL with query string is redirected to root with query string stripped off using ? in the target.

htaccess Rewrite path to subdomain except the Index Document

I have the following structure in my website and I need to redirect all the traffic to a subdomain
Structure:
domain.com/subdomain/
.../folder1/folder/1.txt
.../folder1/folder/2.txt
.../folder2/folder/1.txt
.../folder2/folder/1.txt
.../index.php
I want to redirect all the traffic except the index.php or (domain.com/subdomain/) to a subdomain
Example:
domain.com/subdomain/folder1/folder/1.txt ---> subdomain.domain.com/folder1/folder/1.txt
domain.com/subdomain/folder1/folder/2.txt ---> subdomain.domain.com/folder1/folder/1.txt
But
domain.com/subdomain/index.php ---> No redirect
domain.com/subdomain/ ---> No redirect
This is what I came up with:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [NC]
This works for all the request but I want to exclude both / & index.php from this RewriteRule
Thanks!
You'll need a bit more than you have there. You must first check the domain with RewriteCond. The pattern ^subdomain/?(index\.php)?$ should match requests to the subdomain root or index.php with or without /. Technically it would also match the invalid subdomainindex.php without a / between but that would result in a 404 anyway.
RewriteEngine On
# If the requested domain isn't already the subdomain...
RewriteCond %{HTTP_HOST} !^subdomain\. [NC]
# Rewrite it to the subdomain
# unless it is a request for index.php or /
RewriteRule ^subdomain/?(index\.php)?$ - [L]
RewriteRule ^subdomain/(.*) http://subdomain.com/$1 [L,R=301,NC]