htaccess rewrite subdomain to subfolder - .htaccess

I am trying to achieve getting subdomains pointed to subfolders so for example
http://{subdomain}.example.com points to http://example.com/{subdomain} without changing the URL in the address bar.
I currently have this code:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1 [L]
Which seems to work, but it changes my URL bar aswell
I knnow there are multiple questions about this, but none fitted my awnser.

Try using virtualhosts instead, edit your httpd-vhosts.conf and add this:
<VirtualHost *:80>
ServerAlias *.yourdomain.com #wildcard catch all
VirtualDocumentRoot /opt/lampp/htdocs/%1
UseCanonicalName Off
<Directory "opt/lampp/htdocs">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

You can use this lookahead based rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^.*\.example\.com$ [NC]
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$
RewriteRule ^ %1%{REQUEST_URI} [L]

Related

Apache2 rewrite condition with https

I have 2 .htacces file
/usr/local/apache2/htdocs .htacess file with
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/test2/
RewriteRule ^(.*)$ /test2/$1 [NC,QSA]
/usr/local/apache2/htdocs/test2 .htacess file with
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule !^(ui/|index.php|favicon.ico) - [F,L,NC]
#RewriteCond %{HTTPS} off
#RewriteCond %{REQUEST_URI} !^/admin
#RewriteRule .* https://%{HTTP_HOST}%/admin [L,R=301]
</IfModule>
The application will force the application to the url below while hiding the index.php
http://www.example.com/admin
additionally there is another link with http://www.example.com/display
default directory configured in httpd.conf are :
DocumentRoot "/usr/local/apache2/htdocs/test2"
<Directory "/usr/local/apache2/htdocs/test2"
However if I try to run with https the link
https://www.example.com/admin
I will get this error :
Forbidden
You don't have permission to access /admin/ on this server.
I have tried using the #commented code and still has error. Please help
These problem were fix by applying
I have found out that there are another file, ssl.conf that has a DocumentRoot, fix the problem via adding the same document root in ssl.conf
DocumentRoot "/usr/local/apache2/htdocs/test2"
<Directory "/usr/local/apache2/htdocs/test2"
and
Change a file php.conf to (# was the old code)
# Depreciated in Apache 2.2
# Order allow,deny
# Deny from all
# Satisfy All
# New in Apache 2.4
<RequireAll>
Require valid-user Require ip 127.0.0.1
</RequireAll>

How can i change my my URL with .htaccess on Symfony 2

I would like to change my url using only .htaccess, how can i change my url from this : http://s1rd-ubuntu-01/vpscraplist/web/app_dev.php/myproject/ to this http://vpscraplist/platform/ or this http://vpscraplist/
i have also more pages on my project as http://s1rd-ubuntu-01/vpscraplist/web/app_dev.php/myproject/add and http://s1rd-ubuntu-01/vpscraplist/web/app_dev.php/myproject/edit ...
I've already used thoses scripte but not work for me :(
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^app_dev.php - [L]
RewriteRule ^app.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ /app.php [QSA,L]
RewriteRule ^(.*)$ /app_dev.php [QSA,L]
or this one :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^/web/app_dev.php - [L]
RewriteRule ^/web/app.php - [L]
# Fix the bundles folder
RewriteRule ^bundles/(.*)$ /web/bundles/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ /web/app.php [QSA,L]
RewriteRule ^(.*)$ /web/app_dev.php [QSA,L]
Thank you and sorry about my english !
In my opinion the best approach is to keep the default htaccess of symfony, and to configure your apache vhost as explained there:
http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html
(don't forget to enable the apache rewrite if it is not dude: sudo a2enmod rewrite )
create the vhost file:
sudo nano /etc/apache2/sites-available/yoursite.conf
with:
<VirtualHost *:80>
ServerName mydomain.net
ServerAlias www.mydomain.net
DocumentRoot /var/www/myprojectSymfony/web
<Directory /var/www/myprojectSymfony/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
Once you have have created your virtual file, you must enable them
sudo a2ensite yoursite.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart

Return 404 for the /web directory Symfony 3

All my links are accessibles by mysite.com but also with mysite.com/web/
I would not like to redirect /web to / but, i would like to return a 404 for ^/web. I think it's possible cuz i saw a lot of symfony projects which return 404 error for the /web path
Thanks you all !
You need to modify your apache configuration so the document root points to /web folder.
Your apache config file needs to look something similar to this:
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com
ServerAdmin contact#mysite.com
RewriteEngine On
DocumentRoot your_site_path/web
<Directory your_site_path/web/>
Options FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
</Directory>
</VirtualHost>

301 redirect multiple domains to single domain

I'm using the following in my .htaccess file and works at main level but not subfolders. So the problem is http://mydoamin.com/folder/file.php redirects to the wrong location http://mydomain.com/file.php and the subfolder is lost.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
If you have access to the apache config create another vhost site
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias anotherdomain.com anotherdomain.org ...
RedirectPermanent / http://www.mydomain.com/
</VirtualHost>

Abstraction of Apache VirtualHost URL's

So I have a couple of mod_rewrite rules to make URL's prettier.
By this I mean I wish to change a url that is entered as such:
http://test.domain.com/cheese/wine/
to a background association towards the test.php as such:
http://test.domain.com/test.php?client=test&page=cheese/wine
PS: The client variable above is actually the [test].domain.com.
According to the rewrite log all the RewriteRule's seem to work fine but all I get is a 400 error.
vHost file:
<VirtualHost *:80>
ServerName *.domain.com
DocumentRoot /var/www/dir/
...
RewriteEngine on
# drop www. from subdomains
RewriteCond %{HTTP_HOST} ^www\.(.*)\.domain.com$
RewriteRule ^/?(.*)$ http://%1.domain.com/$1 [R=301]
# rewrite x.domain.com/abc to abc?client=x
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteRule ^/?(.*)$ $1?client=%1 [QSA]
RewriteRule ^/?(.*)$ test.php?page=$1 [QSA,L]
...
</VirtualHost>
Does anyone have any ideas?
Edit: The request hostname (*.domain.com) needs to stay intact because (www.)domain.com is treated in a different manner.
Your code generates an infinite loop.
<VirtualHost *:80>
ServerName *.domain.com
DocumentRoot /var/www/dir/
...
RewriteEngine on
# drop www. from subdomains
# also drop www. from main domain
RewriteCond %{HTTP_HOST} ^www\.(.*\.)?domain.com$
RewriteRule ^/?(.*)$ http://%1domain.com/$1 [R=301]
# rewrite x.domain.com/abc to abc?client=x
RewriteCond %{REQUEST_URI} !^test\.php
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$
RewriteRule ^/?(.*)$ test.php?page=$1&client=%1 [QSA,L]
...
</VirtualHost>
I also modified the code so www.domain.com/ redirect to domain.com/

Resources