I'm trying to make RewriteRule redirect to an absolute path on the server, outside of the current directory. This works when correctly implemented in PHP (i.e. no privilege issue), but with .htaccess, it doesn't:
RewriteEngine On
RewriteRule ^files/(.*)$ /var/www/files/html/$2
This is an example, though. Imagine this .htaccess file is at /var/www/downloads/html and redirects to the other location. Like I said, it's an example and doesn't have to make sense in this case.
This probably doesn't work because it redirects to /var/www/files/html/var/www/downloads/html, I think.
Question: How do I make RewriteRule point to a directory outside of the current one?
Update: The VirtualHost for this site is:
<VirtualHost *:80>
ServerName subdomain.domain.com
DocumentRoot /var/www/files/html
</VirtualHost>
You can change DocumentRoot to /var/www and place your .htaccess there. The rule could then look like
RewriteRule ^files/(.*)$ /downloads/html/$1
But then, everything below /var/www will be publicly accessible. If this is not an option (e.g. security concerns), you must stick to your PHP solution.
Related
I am trying to get all http requests to go to a different local directory than where DocumentRoot points to:
The web server config:
<VirtualHost 1.2.3.4:80>
DocumentRoot /local/home/me/example.com
ServerName example.com
However, the entire (PHP) code for this site is not at DocumentRoot but rather in a subdir:
/local/home/me/example.com/code/
Currently, to access the site, I have to use URLs that include the /code/ subdir. I like to get rid of that so that http://example.com/ accesses /local/home/me/example.com/code/
I understand that the right way is to make DocumentRoot point to the subdir. My web hoster won't let me change this setting, though (in fact, that's how I had it before, when I hosted this site on my private web server where I could set the DocumentRoot just like that).
I now hope I can accomplish this with rewrites in .htaccess file(s). I just don't understand how.
My questions, in particular:
What are the rewrite rules for this?
Can I make a single .htaccess file for all paths, or do I have to have one per subdir (e.g. there are subdirs inside /code/ - do they each need an individual .htaccess file?
You can use this code in your /local/home/me/example.com/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!code/).*)$ code/$1 [L,NC]
(?!code/) is negative lookahead that will check if request already doesn't start with /code/ and will add this if condition is true.
To let people access my website whether they type the www. in front or not where do I place the .htaccess file to do this as I have tried it and it won't work.
I now know that I need to place the .htaccess on the non www. server and need to know how to access this to put it there.
You need to place this file in the root of the folder. Make sure the file is called .htaccess and does not have an extension at the end ie .htaccess.txt.
If the directory in which your website is held for example is
/var/www/mysite
then this is the directory where the file needs to be placed. Depending on how your web server is set up, apache may not allow the use of htacess files. To overcome this do the following.
cd /etc/apache2/sites-available
and open “default” up in your editor or choice, eg
sudo nano default
Default for AllowOverride is none, it should be All, so your overall “default” file should look like this:
NameVirtualHost *
ServerAdmin admin#site.com
DocumentRoot /var/www/
Options FollowSymLinks
AllowOverride None
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
If you do not have ssh access, you may need to speak to your webhost.
Additional Comments
If the htaccess file is confirmed as working then you need to add the following code to the htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This would forward all traffic to http://site.com
Or for the other way around
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Both of these will obviously need you to replace example.com with your site address. This is one way to do this, or you could add a ServerAlias in your apache vhost file for that particular site.
This has nothing to do with htaccess. You need to make sure that when someone types www.example.com, it goes to the exact same place as example.com. This is a DNS issue. Find your domain registrar and set that up.
Then on your server, allow for both in your vhost config. You should see something like this:
ServerName example.com
ServerAlias www.example.com
I tried to make a .htacces to simulate dynamics subdomains.
like his post :
.htaccess: mod-rewrite; subdomain
but it's impossible for me to get the expected result.
i have read several times the doc at : apache
i have no problem with : RewriteCond, RewriteRule, regular expression.
But i have problems with the subdomain.
i have this page : tododiversion.es
and for my example, I want this :
chr.tododiversion.es ==> tododiversion.es/chr/
i put .htaccess in the www folder: www/ .htaccess
and the folder "chr" is in : www/chr/
in the htacces i put :
RewriteEngine On
# host starts with something else
RewriteCond %{HTTP_HOST} ^([^\.]+)\.tododiversion\.es$ [NC]
# rewrite
RewriteRule ^(.*)$ /chr [L]
it doesn't work..
I know that urlrewritting is working because I made a simple test.
I know that RewriteCond is working because I made a simple test.
I was trying on my computer with localhost and 127.0.01 and it didn't work neither.
If someone could give me some advice it should be great!
Chris
The issue could be that apache is not properly configured to handle subdomains. Make sure that you have all subdomains set up to forward to this directory, or the .htaccess will never be executed.
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
DocumentRoot /var/www/domain.com
</VirtualHost>
Remember to restart Apache after updating your config files.
For a reference, see How to configure subdomains for Apache2 on Ubuntu?
You need to include $1 in the rewrite rule so that any URL specifics after the domain are passed on.
chr.tododiversion.es/page.html ==> tododiversion.es/chr/page.html
Based on the link you provided, the rewrite rule should be as follows:
# rewrite
RewriteRule ^(.*)$ /%1/$1 [L]
I just moved my site to a new server and this is Mac OS X server
the problem is that I don't have much control with it and there is one little problem [or maybe big for others]
When people visit my site http://mydomain.com this will work fine with or without www but I've another folder in a sub directory called forum and the only want to visit this site is to go like http://www.mydomain.com/forum and when I visit http://mydomain.com/forum it will result page not found and my current solution is to create a directory in the folder www where my site locate with a folder called forum with redirection to http://www.mydomain.com/forum
Now my question is how can I create a rewrite rule to .htaccess to make it support both version of www and non-www so that the visitors will have an option to visit my site.
Sorry if I post in the wrong place.
Thanks
I use this:
<VirtualHost *:80>
ServerName domain.com
DocumentRoot /u02/web/hosting/domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^/(.*)$ http://www.domain.com/$1 [L,R=Permanent,L]
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain.com
DocumentRoot /u02/web/hosting/www.domain.com
....
</VirtualHost>
First vhosts just redirect all traffic from domain.com to second vhosts www.domain.com
There are many ways to skin the cat, but my instinct says that two different domains with different behaviors should be different VirtualHost entries, possibly with different document roots.
In this case I would make two virtual hosts pointing to the same DocumentRoot since for the most part http://example.com and http://www.example.com are the same site, except that example.com wants to disallow some things.
The VirtualHost for http://example.com can look for specific URL's like /forum and fail them, allowing the others to pass on to accessing the document root.
The VirtualHost for http://www.example.com does not care about any of that; it just allows normal access to its URL.
I am working with Contao (TypoLight).
We have several sites on the same Contao Installation.
Which also means it's the same root folder.
Each site has its own entrypoint, atleast its supposed to have.
Now let's assume I have Site A (www.sitea.com) and Site B (www.siteb.com).
Site A needs to be accessible through www.sitea.com
Site B needs to be accessible through www.siteb.com
Now according to the manual each site configuration should have its own domain name entered in the entrypoint. This I have done but now www.siteb.com is redirecting to www.sitea.com.
Does anyone know why this behaviour is happening?
Or do I need to wait für any DNS updates?
Or do I need to specify any rewrite rules in .htaccess?
Cheers!
for each entry point in your site structure, configure the Domainname without "www":
for sitea.com: sitea.com
for siteb.com: siteb.com
create a htaccess (.htaccess file in your webroot) and configure the Host to redirect from www.site[a,b].com to http://site[a,b].com:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Contao is now able to route properly.
The answer may not be linked with htaccess or whatever. It probably has to do with vhosts.
Here's what you may have done, and if not, this should explain why your configuration doesn't work:
In the httpd.conf file, sometimes you have an include of a vhosts directory.
If not, here's what I do: I setup Apache so that it reads all the vhosts in a specific directory (it's almost at the end of the httpd.conf file, so that the default directives are applied before including vhosts):
# Include Virtualhosts directory:
NameVirtualhost *
Include /web/vhosts/
Then in the dir, I set up all my vhosts:
olivier#Tt /web/vhosts # find . | sort
./labyz.vhost.conf
./olivierpons.vhost.conf
./wipwip.vhost.conf
./wogwog.vhost.conf
olivier#Tt /web/vhosts #
Then for each vhost I precise their own directives. Example (redirects all to http://disneyland.fr/):
<VirtualHost *>
ServerAdmin webmaster#olivierpons.fr
DocumentRoot "/web/htdocs/olivierpons/prod"
ServerName olivierpons.fr
ServerAlias *.olivierpons.fr
ErrorLog "/web/logs/olivierpons.error.log"
CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/olivierpons.fr/access.%Y-%m-%d-%H_%M_%S.log 5M" combined
RewriteEngine On
RewriteRule (.*) http://disneyland.com$1 [QSA,R=301,L]
</VirtualHost>
Note: in your case, the vhost files sitea.com.vhost.conf and siteb.com.vhost.conf have the same DocumentRoot.
Have you done that this way?