.htaccess 2 domains, same target, 1 redirect - php

I own 2 domains that are pointing to the same static ip. I wanted to set domain1.com to be redirected to domain1.com/down.php but only for the target domain1.com and domain.com/index.php/html.
The domain2.com should work as usual, so no redirects here. It should target /var/www/html like it currently does.
Background is: that there was a project which should not be available on the domain1.com, but there are specific files such as domain1.com/subdir/subdir/file1.jpg, which should still be accessible there.
Any ideas?
Many Thanks

.htaccess to redirect domain1.com/*
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ down.php?_url=/$1 [QSA,L]
The rule only works if the condition is meet. ?_url=/$1 is not needed but may prove useful.

If you use htAccess, also it seems you use Apache2
You can also use virtual host directly from apache 2 configuration :
https://httpd.apache.org/docs/current/vhosts/examples.html

Related

Manage two domains pointing to one hosting with htaccess

Well, I want to know somebody me to explain, if possible, how could I manage one hosting to have two domains pointing to it.
My purpouse is to have two folders, and manage it with htaccess.
I have two parked domains in Hostinguer, now I want to know how can I make that the htaccess only allow one folder to one domain and the other folder to another domain, and take this to all the directories, because now, I can access my webpage by the two domains and I want to know how can I solve it.
My first try was this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|)\.domain\.com$ [NC]
RewriteRule ^ - [F]
I know more or less what is every thing, there is a little bit of Regex and some instructions that says how it has to works.
Let's say something like that in PHP:
if($_SERVER['SERVER_NAME'] == "www.domain.com") //There is an regex I know
exit; //Or, in this case throw a 403 error.
But, for example, I don't know how can I apply this action to all the subdirectories, avoiding this two-three lines to every .htaccess in every folder.
And also, It didn't work as I expected, because, it throw me a 403 error if I visit it from my domain.com, but also, it throw me a 403 error if I visit it from my subdomain: http://ikillnukes.hol.es/fapi (I will leave it running all the night to prove it)
Note: I don't have access to the Apache, so, don't suggest me to make an virtualhost because I can't. ;-)
EDIT:
I have now:
domain1.com, domain2.com and sub.domain1.com
And those folders:
root
domain1 //This is created by me
sub //This is automatically created by Hostinger, and I can't change the name or the path
domain2 //This is created by me
So, there is any problem by doing what #Walf suggested to me, but I have the problem of the subdomains and the .htaccess #Walf provided to me is a little bit compilcated to understand (my fault), so, what can I should try?
So, any help to me explaining how to approach this would be fantastic!
Thanks in advance!
Your question is probably a duplicate but I can't find one that uses adjacent dirs (as I think you're describing), nor one with a solution I'd use, myself. Try this in your root .htaccess:
RewriteEngine on
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to a dir with the same name (ignoring www)
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
# see if first dir in url doesn't match domain
RewriteCond %1 =!^(?>([^|]+)\|\1)$
# capture first dir (if any) to check against above conditions and pass through as new url if not prefixed with domain dir
RewriteRule ^[^/]* %1%{ENV:REQ} [NE,DPI,PT]
Your dir structure would then be
docroot/
domain1.com/
whatever
domain2.net/
whatever
.htaccess
You should then be able to have normal .htaccess directives in each subdir, if you wish. Ensure you use RewriteBase / in those dirs.
Re-edit That just makes your situation more like most others'.
RewriteEngine on
RewriteBase /
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to the required dir
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
RewriteRule ^ - [E=DOM:%1]
# explicitly set dir per host
RewriteCond %{ENV:DOM} =sub.domain1.com [NC]
RewriteRule !^sub/ sub%{ENV:REQ} [NE,DPI,L]
RewriteCond %{ENV:DOM} =domain2.com [NC]
RewriteRule !^d2/ d2%{ENV:REQ} [NE,DPI,L]
# allow domain1.com to proceed to root (any other rules go below)
# rules must still exclude subdirectories for other domains, e.g.:
RewriteRule ^(?!sub/|d2/)([^/]+)/([^/.]+)$ foo.php?bar=$1&baz=$2 [NE,B,L,DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(?:sub/|d2/|index\.php$) index.php [L,DPI]
# after all other rules, emulate DirectorySlash so that Apache does not naively insert hidden directory into public URL
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?>.*)(?<!/) %{ENV:REQ}/ [L,DPI,R]
If you're unsure of any regex, paste it into regex101.com (with the g flag off) and look at the explanation.

htaccess mod_rewrite redirect to directory without displaying on url

I have a site called www.example.com and I have my php files in it. I store all my working files in www.example.com/site. I want to view the site in www.example.com instead, without moving my site content. What can I do?
This is currently what I am typing in .htaccess. It will redirect my site to www.example.com/site but I think the url is ugly
RewriteEngine on
RewriteBase /
RewriteRule ^(/.*|)$ /magento$1 [NC,L]
Let me see if i understood this correctly.
You have a domanin, www.example.com, and on this domain you want to display the content of a directory, www.example.com/site !?
If this is the case then you need to change the document root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]
or
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /site/$1 [L,NC]
There are 2 things you must consider :
If I understand your request, you are accessing some PHP files using www.example.com, but what you want is to access www.example.com/site, but without the /site, right ?
So basicaly, what you're looking for is NOT rewrite, it's just pointing your domain to the good folder, which is /site, right ?
If you're using Apache2, you have to edit your apache's configuration file in /etc/apache2/site-available/default (or remplace default with the name of the virtualhost you may have created).
In this file, look for the directive DocumentRoot. It should lead to the "root" directory of your pages (the one you access typing www.example.com)
I think you just have to append /site to this DocumentRoot and then reload your apache2 with service apache2 reload
You're website www.example.com will now lead to the correct directory.
If it's still not working, you must consider looking into magento's admin, because magento is rewriting url according to the Base URL you specify inside Admin / Configuration / general / web.
You'll have to modify Base_URL in both Secure and Un-Secure sections.
Then it should work fine.
I would comment out the Rewrite bloc you're using at the moment, or maybe I didn't fully undestand what you want to achieve.

Sub Domain Name Creation in .htaccess

I have an application on SAAS basis where I will be adding clients from backend or the clients can even create their own panel by signing up
While signing up or adding the client , the client or I will be assigning the sub domain . So suppose I am assigning subdomain xyz to client1 . Then the access url will be
xyz.maindomain.com
Now , I want .htaccess code in such a way that if xyz.maindomain.com is called, the url called must be maindomain.com?client=xyz .
Please guide me how to do this.
Thanks in advance.
We could use mod_rewrite to achieve what you wanted. No server-side scripting such as PHP required.
Say, put this in .htaccess file on your root www directory (or any relevant Apache *.conf file if you wish)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/?client=%1/$1 [L,R=301]
This will redirect foo.example.com to http://www.example.com/?client=foo
(change example.com to your domain name)
More about mod rewrite, with examples:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
Hope this help.

Mod rewrite base URL

I have several domains that I want to all rewrite to one domain. I don't want it to redirect because I want the URL to look like what the user has entered in. For example if they enter www.example.com I want it to load the page from www.sample.com/default.php?from=example
I have worked a little with rewriting if you have www.site.com/var1/var2 making it load www.site.com/index.php?one=var1&two=var2
Is it possible to do what I am looking for just through the .htaccess file? I tried looking around and couldn't exactly find what I was looking for
Thanks
If the sites are hosted on different servers or don't share a common document root, then you'll have to rely on mod_proxy and you can use the P rewrite rule flag. For example, these rules in an htaccess file in www.example.com's document root:
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ http://www.sample.com/default.php?from=%1 [L,P]
Will take the request http://www.example.com/ and invisibly proxy it to http://www.sample.com/default.php?from=example. The browser's URL address bar will remain http://www.example.com/.
Note that the rule only matches against the request URI /. If you want to do more, you'd have to create the correct regular expression and grouping.
If you have redirects on the sample.com site, you'll need to employ ProxyPassReverse to rewrite the redirects. Also see ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath if there are cookies involved.
If you can do this in vhost or server config instead, then consider simply using ProxyPass instead of mod_rewrite. The ProxyPass directive won't work inside htaccess files.
EDIT:
Seeing as how everything is in the same document root, you won't need to proxy anything. Simply:
RewriteCond %{HTTP_HOST} !^www\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ /default.php?from=%1 [L]
I use online generators.
Use this generator: mod-rewrite

htaccess rewrite ".../pages/about.php" to ".../about"

I've searched and found a lot of questions on this site and elsewhere that are very similar, but I've tried implementing and modifying all the suggestions I've found and none of it works. I realize this is a very basic question an I am extremely frustrated because nothing I'm trying is working.
With that having been said... I am trying to organize my content pages within kurtiskronk.com/pages/... (e.g. kurtiskronk.com/pages/about.php)
What I want to do is make it so that I can simply link to kurtiskronk.com/about ... So how do I go about stripping "pages/" and ".php"? I don't have a ton of content pages, so it's not a big deal if I have to specify for each page, though something dynamic would be handy.
NOTES: I am using Rackspace Cloud hosting, and WordPress is installed in /blog. My phpinfo() can be seen at http://kurtiskronk.com/pages/phpinfo.php
This is my existing .htaccess file (in the root)
php_value register_globals "on"
Options +FollowSymLinks
RewriteEngine On
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
RewriteBase /
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{ENV:PHP_DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
# PHP - MAIL
php_value mail.force_extra_parameters -kurtis#kurtiskronk.com
I tested and the rewrite works with the line below (/about as URL brings up file /pages/about.php), but then the homepage gives a 500 Internal Server Error:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
So I'm still sort of in the same boat as before, and as a follow-up, possibly more difficult question, if you go to http://kurtiskronk.com/weddings I am using SlideShowPro (flash) w/ SSP Director (self-hosted) as the back-end for it. When it pulls up a new image, it adds the following after /weddings ... "#id=album-152&num=content-9698"
There are four sections of the portfolio
# Homepage (kurtiskronk.com) id=album-148 ($id is constant for this section)
# Weddings (/weddings) id=album-152 ($id is constant for this section)
# Portraits (/portraits) id=album-151 ($id is constant for this section)
# Commercial (/commercial) id=album-150 ($id is constant for this section)
Assuming we get kurtiskronk.com/weddings to rewrite successfully without breaking anything, how would we make the total URL something cleaner kurtiskronk.com/weddings/9698 since the $num is the only thing that will change within a given section?
Kurtis, thanks for the extra information. It's a lot easier to give a specific answer to this.
My first comment is that you need to separate out in your thinking URI space -- that is what URIs you want your users to type into their browser -- and filesystem space -- what physical files you want to map to. Some of your mappings are URI->URI and some are URI->FS
For example you want to issue a permanent redirect of www.kurtiskronk.com/* to kurtiskronk.com/*. Assuming that you only server the base and www subdomains from this tree, then this cond/rule pair should come first, so that you can assume that all other rules only refer to kurtiskronk.com.
Next, you need to review the RewiteBase documentation. .htaccess files are processed in what Apache calls a Per-Directory context and this directive tells the rewrite engine what to assume as the URI base which got to this directory and .htaccess file. From what I gather, your blog is installed in docroot/blog (in the filesystem, and that you want to get to directory by typing in http://kurtiskronk.com/blog/ but that this .htaccess file is for the root folder -- that is the base should be (this goes before the www mapping rule)
DirectorySlash On
DirectoryIndex index.php
RewriteBase /
#301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www\.kurtiskronk\.com$ [NC]
RewriteRule ^(.*)$ http://kurtiskronk.com/$1 [R=301,NC]
You can add some field dumps look for REDIRECT_* in the Server or Environment table in the phpinfo O/P to see if these are sensible. For example:
RewriteWrite ^(.*)$ - \
[E=TESTDR:%{DOCUMENT_ROOT}/pages/$1.php,E=TESTPDR:%{DOCUMENT_ROOT}/pages/$1.php]
Your next rule is that if the file exists in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
RewriteRule ^(.*)$ pages/$1 [NS,L]
[Note that some shared service sites don't set up DOCUMENT_ROOT properly for the rewrite engine so you may need to run a variableinfo script (<?php phpinfo(INFO_ENVIRONMENT | INFO_VARIABLES); to see if it sets up alternatives. On your site you have to use %{ENV:PHP_DOCUMENT_ROOT} instead.]
Your next rule is that if the file exists, but with the extension .php in the subdirectory pages then use it:
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^(.*)$ pages/$1.php [NS,L]
Now redirect any blog references to the blog subdirectory unless the URI maps to a real file (e.g. the blog stylesheets and your uploads.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/ blog/index.php [L]
A complication here is that WP may be using a poorly documented Apache feature call Path Info that is a script can act as a pseudo directory so http://kurtiskronk.com/blog/tag/downtown/ is redirected to docroot/blog/index.php/tag/downtown/ which is then executed by `docroot/blog/index.php using /tag/downtown/ as the PATH_INFO. But this is one for Wordpress experts to comment on. If this last rule doesn't work then try:
RewriteRule ^blog/(.*) blog/index.php/$1 [L]
PS. I like your site. I wish I was that young again :(
Postscript
When you say "it doesn't work", what doesn't with this .htaccess?
http://kurtiskronk.com/phpinfo,
http://kurtiskronk.com/phpinfo.php,
http://kurtiskronk.comblog/tag/downtown/
It's just that these rules work for these tests (with domain swapped) on mine. (One way is to move or copy the above variableinfo.php to the various subdirectories. If necessary temporarily rename the index.php to index.php.keep, say, and copy the variableinfo.php to the index.php file. You can now enter the various URI test patterns and see what is happening. Look for the REDIRECT_* fields in the phpinfo output, and the SCRIPT_NAME will tell you which is being executed. You can add more {E=...] flags to examine the various pattern results. (Remember that these only get assigned if the rule is a match.
Lastly note the changes above especially the additional NS flags. For some reason mod_rewrite was going directly into a subquery which was resulting in redirect: being dumped into the file pattern. I've had a look at the Apache code and this is a internal botch to flag that further redirection needs to take place (which then replaces this or backs out). However this open bug indicates that this backout can be missed in sub-queries and maybe that's what is happening here. Certainly adding the NS flas cured the problem on my test environment.
PS. Note the added explicit DirectoryIndex directive and also that whilst http://kurtiskronk.com will run the root index.php, the explicit /index.php version will run the one in pages, because that's what your rules say.
Here is a simple solution. You can use it apache conf file(s) or in .htaccess (easier to set up when you're trying).
mod_rewrite has to be enabled.
For example, use .htaccess in your DocumentRoot with:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /pages/$1.php [L]
It will redirect /about to /pages/about.php, and any other page.
The "RewriteCond" part is to authorize access to an existing file (eg: if you had an "about" file at the root of your site, then it will be served, instead of redirecting to /pages/about.php).
Options +FollowSymLinks
RewriteEngine On
RewriteRule /([0-9]+)$ /pages/$1.php [L]
Put something like this in your .htaccess file. I guess that is what you want.
Juest a redirect from a simple url to a longer url.

Categories