I try to read many of the answer regarding htaccess but I could not find my way out.
Image this architecture in webhosting: root/public_html/subfolder
Now with the following code in htaccess file (placed in public_html) I can manage that when the domain is type in browser the visitor land in subfolder without digit example.com/subfolder, example.com is enough. It works.
What I would like now is that the visitor will be redirect not in subfolder but in an "extrafolder" placed inside the root (level root that is the same level where the folder public_html is placed but not inside public_html).
So how to modify this code:
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subfolder/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subfolder/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /subfolder/index.html [L]
I try changing the /subfolder/ with ../extrafolder/ (supposing the double dot will push up one level) but does not work.
Any idea?
Thank you
The current configuration you put is just working in case you put .htaccess inside the root folder of your website, which in this case is public_html, so no way to access another folder outside that root, except if you have directory alias.
In case you have access to change your apache configurations
You need to have the following steps to completely control the root of your website.
1- Yur apache virtual host configuration (this should be in /etc/apache2/sites-enabled/example.conf) should be something like the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test
<Directory /var/www/test/>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
3- You need to add this Alias /externalfolder/ "/var/www/test/externalfolder/" to the virtual host configuration above
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test/public_html
Alias /externalfolder/ "/var/www/test/externalfolder/"
<Directory /var/www/test/public_html>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now you can access that via http://example.com/externalfolder/file.php
Related
I have what I thought to be a very simple and classic use-case of mod_rewrite that is stumping me.
I have a file structure like this:
/var/www/site
/var/www/site/webapp <-- js application
/var/www/site/index.php <-- php entry point
/var/www/site/.. other folders..
I want my users to navigate to "mysite.com" which will load the contents of the webapp directory, therefore I believe it should be the DocumentRoot. I cannot for the life of me get this working while keeping the rewrite for the PHP application/router functional.
My current VirtualHost configuration works, but the browser must be pointed to site.dev/webapp; navigating to site.dev shows a directory list.
<VirtualHost *:80>
ServerName site.dev
ServerAdmin me#localhost
DocumentRoot /var/www/site/
LogLevel core:debug
ErrorLog ${APACHE_LOG_DIR}/site.error.log
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [QSA,L]
</Location>
</VirtualHost>
Desired functionality is:
http://site.dev/ loads content from /webapp
http://site.dev/order/18 redirect to index.php passing /order/18 from URI
ErrorDocument 404 /pageNotFound.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/([0-9a-zA-Z-_]+)/message/?$ message.php?user=$1 [NC,L]
#
#RewriteRule ^profile/([0-9a-zA-Z-_]+)/([0-9a-zA-Z-_]+)/?$ profile.php?user=$1&content=$2 [NC,L]
#
RewriteRule ^profile/([0-9a-zA-Z]+)/?$ profile.php?u=$1 [NC,L]
#
RewriteRule ^story/([0-9a-zA-Z-_]+)/?$ blog_post.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/edit/?$ question_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/?$ question.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/edit/?$ contribution_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/?$ contribution.php?url=$1 [NC,L]
#
RewriteRule ^forums/tags/([a-z-]+)/?$ allTags.php?tag=$1 [NC,L]
#
Above is my .htaccess file and I am using WAMP.
At first when I did this, everything works fine, but later on, some URLs started misbehaving for example if I type in localhost/profile/debbie after the page finishes loading the url changes to localhost/profile/Debbie/?u=debbie, though the page contents work fine. I just don't know why. But if I do something like localhost/profile/the_king the url remains the same after loading.
But later on the if I do this localhost/profile/the_king I started getting an error message of undefined index u
Secondly I have a Subdomain under WAMP for my mobile site which uses an IP address of 192.168.106.1 and I use a duplicate of the above .htaccess file for it, but if I do this 192.168.106.1/profile/the_king it just get back my pageNotFound.php error page back.
This is really getting me angry because I watched some video tutorials and I did exactly what they did but I just don't know why mine is misbehaving
Edit here is my configuration for my Subdomain
# Tells Apache to identify which site by name
NameVirtualHost *:80
# Tells Apache to serve the default WAMP Server page to "localhost"
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www/"
</VirtualHost>
# Tells Apache to serve Client 1's pages to "client1.localhost"
# Duplicate and modify this block to add another client
<VirtualHost 127.0.0.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.56.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/"
# A few helpful settings...
<Directory "C:/wamp/www/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.106.1>
# The name to respond to
ServerName m.localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
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 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?
I've domain using wildcard dns but I want my main site example.com will grab content from folder /home/ by example.
If user type http://www.example.com/
or http://example.com/ site will read
file from subfolder /public_html/home/
If http://subdomain.example.com/ will reading content from /public_html/.
Let me know how to that in my .htaccess.
Try this rules:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule !^home/ home%{REQUEST_URI} [L]
you don't need htaccess or mod-rewrite for that. just config your site's document root dir to be /var/www/home/ or whatever you want it.
for example, in apache2 you can put this in apache-dir/sites-enabled/000-default/
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin mail#domain.com
ServerName domain.com
DocumentRoot /var/www/home/
<Location "/">
allow from all
</Location>
</VirtualHost>