.htaccess rewrite but keep start URL - apache

I have main domain, for example, domain1.com. Also I have domain2.com which is addon domain, and I want to redirect domain2.com to domain.com?parameter=value, which is basically domain1.com, but with another template, but that path in URL stays same, domain2.com, not domain1.com. .htaccess file is created inside addon domain directory (public_html/domain2.com), and I have these two lines:
RewriteCond % ^domain2.com
RewriteRule ^(.*) http://domain1.com?parameter=value [P]
Rewrite works, but URL in browser changes to domain1.com, also I need to rewrite all request, that comes to domain2.com and www.domain2.com. Now only request that com to non-www domain works, but URL in browser changes.
EDIT: U have this code to .htacces, but URL change to domain1.com, instead to stay domain2.com.
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^/?$ "http\:\/\/domain1\.com\/\?parameter\=value" [R=301,L]

Try this in your domain2/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain2\.com$ [NC]
RewriteRule (.*) http://www.domain2.com/$1 [R,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://domain1.com/$1 [R,QSA,NC]

Related

Section a site served through a central controller script into two context dependant subdomains

I have an Apache server that passes requests for all non existing resources to index.php to act as central controller. The htaccess rule for this is:
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
On this server I have two subdomains pointing at the same webroot directory:
www.example.com
and
booking.example.com
My client would like the booking section (/book) to served up using the booking subdomain, while the rest of the site should stay on www.
So they would like URLs such as
https://www.example.com/about-us
and
https://booking.example.com/book
This means that requests to https://booking.example.com/about-us need to be redirected to https://www.example.com/about-us, and along the same lines, request to https://www.example.com/book need to be redirected to https://booking.example.com/book.
Effectively they want the site sectioned off into two subdomains, served from the same system behind the scenes.
The first part (for /book onto booking) is achieved easily enough by simply adding this to htaccess:
# redirect all requests on www. made to /book to booking subdomain
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^book(.*)$ https://booking.example.com/book$1 [L,R=301]
The counterpart (send non /book requests to www) is easy enough as well:
# redirect all requests on booking. made to not /book to www subdomain
RewriteCond %{HTTP_HOST} ^booking\.
RewriteCond %{REQUEST_URI} !^/book
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
In simulation, this works exactly as intended:
https://htaccess.madewithlove.be?share=990cff3e-7739-5bff-912f-493ae76e3a4c
https://htaccess.madewithlove.be?share=a005713e-97bf-59af-b89a-f2c5ceae13ed
Problem is that the index.php redirect is messing this up and I don't fully understand how.
If I only have one of the two rules active they work fine, respectively.
However, if I activate both and go to https://www.example.com and follow a link to https://www.example.com/book I end up at https://www.example.com/index.php instead of the expected https://booking.example.com/book.
As far as I can tell this is because after redirecting to the booking subdomain the REQUEST_URI check from the second rule, which looks for requests on booking for not /book, seems to have the value of REQUEST_URI down as index.php.
What I don't get is why this second check looks at REQUEST_URI with that value - I would have assumed that the [L,R=301] from the first rule triggers a new request to the server that is evaluated as such but this does not seem to be the case.
Instead it appears that the internal resolution of the request to index.php is passed to the second rule.
How can I work around this?
EDIT:
For clarity - the rewrite section of the htaccess looks like this:
# redirect all requests on www. made to /book to booking subdomain
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^book(.*)$ https://booking.example.com/book$1 [L,R=301]
# redirect all requests on booking. made to not /book to www subdomain
RewriteCond %{HTTP_HOST} ^booking\.
RewriteCond %{REQUEST_URI} !^/book
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Try changing:
RewriteCond %{REQUEST_URI} !^/book
to
RewriteCond %{THE_REQUEST} !\s/book
Right so I got this to work by adding a RewriteRule for the the second redirect - booking to www for requests to not /book - to not trigger on /index.php.
Also note that I added a rule to allow resources (js, css) to be loaded on the booking subdomain.
# redirect all requests on www. made to /book to booking subdomain
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^book(.*)$ https://booking.example.com/book$1 [L,R=301]
# redirect all requests on booking. made to not /book to www subdomain
RewriteCond %{HTTP_HOST} ^booking\.
RewriteCond %{REQUEST_URI} !^/book
RewriteCond %{REQUEST_URI} !^/index.php #don't fire on index.php
RewriteCond %{REQUEST_URI} !(.js|.css) #allow js / css
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

htaccess redirect all subdomains to the same directory

I want to be able to redirect all subdomains to a folder:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule (.+)$ "http://example.com/subdomains/%1" [L,P]
for example, if some visits sub1.example.com it will keep the URL but show example.com/subdomains/sub1 and if the sub1 directory does not exist, it will show example.com/404
Is this possible?
I tried the above code but its showing me:
Forbidden
You don't have permission to access /index.php on this server.
Wordpress says:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
and at the top of my htaccess file, is:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} admin.domain.com$
RewriteRule ^(.*)$ /admin/system/$1 [L]
Your above .htaccess would externally redirect the calls, as you use a full URL as the target.
In your question you say you want to keep the hostname, so I will assume that is the requirement.
0) Enable rewrite engine
RewriteEngine On
1) Rewriting known subdomains to their directory in /subdomains
# Rewrite known subdomains to /subdomains/{subdomain}/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteRule ^(.+)$ /subdomains/%1/ [L]
When we encounter a request with a subdomain,
and we have not rewritten it to /subdomains
and we have not rewritten it to /404
then rewrite it to /subdomains/{subdomain}/
So, if the request was
http://foo.example.com/hello
the URL in the browser would stay the same, but internally be mapped to
/subdomains/foo/
2) Rewriting unknown subdomains to /404
# Rewrite missing unknown subdomains to /404/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} ^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /404/ [L]
When we encounter a request with a subdomain,
and we have already rewritten it to /subdomains
and we have not rewritten it to /404
and it is not existing as a file
and it is not existing as a directory
and it is not existing as a symlink
then rewrite it to /404
So, if the request was
http://bar.example.com/hello
the URL in the browser would stay the same, but internally be mapped to
/subdomains/bar/
by the first RewriteRule from 1).
If /subdomains/bar/ does not exist, the second RewriteRule from 2) will kick in and internally map it to
/404/
3) Test-Environment
I actually tested all of this with exemplary code, available here: https://github.com/janpapenbrock/stackoverflow-36497197
I'd say you are experiencing a permission issue. I guess your Apache server runs as apache user. Use chmod to give apache access to this path.

Redirect secured domain to secured subdomain using htaccess

I have 3 domains and one hosting. I am trying to use the same hosting for all of my three domains through htaccess. So, I created a subdomains with that name on my domain linked with hosting, which looks something like below.
www.site1.com [Main domain linked with hosting]
site2.site1.com [subdomain for www.site2.com]
site3.site1.com [subdomain for www.site3.com]
What I want to achieve is, user shouldn't go to subdomain site2.site1.com, instead they would be able to go to www.site2.com only and request will be sent to site2.site1.com at backend.
Up to here, all is done and worked well. The only problem comes afterwards, when I adds SSL on site. I have SSL for all of these domain and subdomain. If a user visit non-ssl, then he should be redirected to SSL one. Some of SSL works well but when I add SSL for all of them, then I start getting 500 error.
Here is my .htaccess file
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^site2.com
RewriteRule ^(.*) https://site2.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^(.*) https://site2.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^site3.com
RewriteRule ^(.*) https://site3.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^www.site3.com
RewriteRule ^(.*) https://site3.site1.com/$1 [P]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
As I add https on redirection link, so it start giving me 500 error whereas when I make redirection to http then it will not load my page because non-secured site http://site2.site1.com will be loaded over secured https://www.site2.com and in a result, nothing will be shown.
Here I need help to resolve this problem. I have looked over different questions but haven't found any question relevant to me because I need to keep my .htaccess working with redirection and SSL. Moreover, I also need to redirect to www one, if not added in URL.
Any help will be appreciated.
Like, all the times I experienced here I have resolved my problem myself. The resolution to my problem was the following htaccess
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^site2.com
RewriteRule ^(.*) site2/$1
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^(.*) site2/$1
RewriteCond %{HTTP_HOST} ^site3.com
RewriteRule ^(.*) site3/$1
RewriteCond %{HTTP_HOST} ^www.site3.com
RewriteRule ^(.*) site3/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
In this way instead of pointing the URL to subdomain having https I have reference that to the directory instead because path will be same for that, and now when I enter the URL with https page opens and don't give any error.

Apache redirect not working

I have two domains example.com and example.net using the same .htaccess file in public_html directory. I want any valid url like example.net/valid-url to redirect to example.com/valid-url.
I tried the following:
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
rewriterule ^(.*)$ http://www.example.com/$1 [R=301,L]
However the redirect is happening (with above code) to example.com not,
example.com/valid-url.
Is there something I'm doing wrong?
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache before testing this.

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.