Persist VirtualHost RewriteRule in httpd.conf - linux

i have edited the httpd.conf with a RewriteRule, it looks like this:
<VirtualHost myip:80>
ServerName mysite.com
ServerAlias mail.mysite.com www.mysite.com
RewriteEngine on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
JkMount /* mysite
...
It works fine, but when the httpd.conf is rebuilded every change i made is lost. I tried editing the pre_virtualhost_global.conf but with no success, if anybody could tell me exactly what i have to put and where i would be very grateful.
Thank you for your time.

You mentioned that there is a pre_virtualhost_global.conf file, so I assume that you have WHM/cPanel. In order to modify Apache virtual hosts you have use include files as described in WHM/cPanel's official documentation.

Related

RewriteRule to an absolute path?

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.

How to remove /current link in capistrano

I was using Capistrano to deploy my source code to beta and live server.
when I deploy it to the beta server the URL looks like deploy.abc.com/project_name/current
I just want to know the way to remove /current from the URL so it will look like deploy.abc.com/project_name. I know this should be done using .htaccess and mod-rewrite. But I have no Idea.
You can modify the VirtualHost (maybe in etc/httpd/vhosts/*.conf):
<VirtualHost *:80>
DocumentRoot /home/deploy.abc.com/www/current #this line change the url
#some code
</VirtualHost>
or the .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
#some code
RewriteRule ^(.*)$ current/$1 [QSA,L]
</IfModule>

Redirect non www. URL to URL with www

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

.htaccess with dynamics subdomains

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]

Contao: Multisite & Multiple Domain Names

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?

Resources