.htaccess redirect path & subdomain as query parameter - apache

I want to redirect a path to a file as query parameter.
For ex:
http://subdomain.example.com/helloworld/
Should redirected to
/index.php?domainParam=subdomain&param1=helloworld
I have tried multiple ways but they either helps with subdomain or with the path. But I want both of them to be redirected as a parameter in a query string.
Like:
RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ index.php?domainParam=%1 [L]
RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteRule ^([^/]+)/? index.php?source=$1 [L]
But it is not helping when redirecting both of them as parameters.
Need your assistance to merge them both.

You may use this rule for this task:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(?!app\.)([^.]+)\.example\.com$ [NC]
RewriteRule .* index.php?domainParam=%1&param1=$0 [L,QSA]

Related

HTACCESS - Allow access to folders

This is my current htaccess.
Site
|.htaccess
|--/folder-1
|--/folder-2
Now my root htaccess is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site.com$
RewriteRule ^/?$ /public/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*) /public/$1 [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I want users to access all files and folders inside folder 1 and folder 2. How that can be done?
Several changes: do your domain redirect first (note the subtle changes);
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!(?:folder-1|folder-2)(?:$|/)|public/(?:index\.php)?$).* public/$0 [L]
This incorporates the folders you're excluding from rewriting into the main rule, so should be more efficient. It looks ugly since it starts with a negative look-ahead assertion (?! and the rest of the groups begin with (?: so they are non-capturing.
Do you really need a single domain? If so, it should be checked on all requests. If not, drop the first RewriteCond of the second RewriteRule.
if your default document is not index.php, change it in the negative look-ahead assertion of the second RewriteRule.

.htaccess redirect with 2 parameters

I am trying to get the following redirect done.
website.abc/index.php?page=foo // With one parameter
website.abc/foo
website.abc/index.php?page=foo&article=bar / With a second parameter
website.abc/foo/bar
And it has to be redirected backwards as well.
If i type website.abc/foo/bar it should internally being redirected to index.php?page=foo&article=bar
I can get it to work with one parameter, but as soon as i'm using the second, the URL looks like this and gives me the 404:
website.abc/foo&article=bar.
I have tried many different ways of making it possible, but nothing worked.
Does anyone know how to do this?
Go like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php [NC]
RewriteCond %{QUERY_STRING} \bpage=([^&]+)\b [NC]
RewriteCond %{QUERY_STRING} \barticle=([^&]+)\b [NC]
RewriteRule .* /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_URI} ^/index\.php [NC]
RewriteCond %{QUERY_STRING} \bpage=([^&]+)\b [NC]
RewriteRule .* /%1/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+)/?)?$ index.php?page=$1&article=$2 [L,NC]

Redirect to new url except for on page with existing expression engine redirect

I have the following redirects in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} /content [NC]
RewriteRule (.*) http://subdomain.domain1.co.uk/index.php/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/content$ [NC]
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]
The first redierect needs to be there for the expression engine to work properly. Visit to
subdomain.domain1.co.uk/content matches the rule but I end up in the indefinite redirect loop. Can anybody help ?
Thanks,
EDIT: 2nd and 3rd rule may actually be incorrect. What I want it to do is to redirect anything from http://subdomain.domain1.co.uk/$1 to http://www.domain2.co.uk/$1 expect for http://subdomain.domain1.co.uk/content and http://subdomain.domain1.co.uk/content/*
Keep your rules like this:
RewriteEngine on
# redirect everything except content/? to www.domain2.co.uk
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain1\.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !\s/content/?[\s?] [NC]
RewriteRule ^ http://www.domain2.co.uk%{REQUEST_URI} [R=301,L,NE,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

Apache Redirect - same domain different REQUEST_URI

I need multiple RewriteCond for the same domain, but different REQUEST_URIs
I need to redirect to a different domain depending on the REQUEST_URI.
I am trying this with no luck:
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuir/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ / [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example2.com/$! [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myotheruniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example3.com/$! [R=301,L]
I thought the [OR] on the request uri condition would work, but it did not. I have also tried without the [OR] and it did not work!
For example:
if the url is:
http://example.com/myuniqueuri
needs to redirect to:
http://example2.com
if the url is:
http://example.com/myotheruniqueuri
needs to redirect to:
http://example3.com
where example2 and example3 can be anything but are not the same.
Any help is greatly appreciated
Thanks
I think the problem might be in your syntax. The reference is $1 not $!. Also the ! is being used incorrectly. This basically means not. Like if not this directory then etc. Well it appears you want to match the directory and then redirect based on that. Try this code and see if it works for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/myuir
RewriteRule ^(.*)$ / [R=301,L]
RewriteCond %{REQUEST_URI} ^/myuniqueuri
RewriteRule ^(.*)$ http://example2.com/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/myotheruniqueuri
RewriteRule ^(.*)$ http://example3.com/ [R=301,L]

.htaccess mod_rewrite based on domain

I have the following rule in my htaccess, I need to modify it to add variable to the end of the re-written url. Basically I have two domains pointing to the same hosting, and I am showing different sites depending on which domain it used.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^(.*)$ /rrr/$1 [L]
If someone goes to rrr.me or www.rrr.me they get the index.php in the rrr directory of my website. This works great!
I now need to be able to add a variable to the index.php. So someone will type in rrr.me/abc and it will append the abc to the end of the index.php in this fashion index.php?var1=abc
NEW REWRITE RULES AFTER ADVICE FROM Olaf Dietsche IN COMMENTS.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^.*$ /rrr/index.php?rrr=$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]
To append the requested URL as a query string
Rewriterule ^.*$ /rrr/index.php?var1=$0 [L]
To exclude the assets directory, you must use an additional RewriteCond
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^(.*)$ /rrr/$1 [L]
For the assets files, you can use a similar redirect
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]