Apache vhost double slash on redirects - apache

I was trying to force the FQDN and HTTPS via Apache Vhost redirects in a Laravel application behind a reverse Proxy. My problem now is that the user sometimes gets redirected to a URL with double slash e.g. https://exampledomain.xy//test instead of https://exampledomain.xy/test and I'm not sure why.
Here is my apache vhost configuration:
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/public
<Directory /var/www/html>
AllowOverride All
</Directory>
ErrorLog /dev/stderr
TransferLog /dev/stdout
RewriteEngine on
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1
RewriteRule (.*) https://exampledomain.xy/$1 [L,R=301]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot "/var/www/html/public"
ServerName exampledomain.xy
</VirtualHost>
And my .htaccess file (default .htacces file that comes with Laravel)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Okay looks like I fixed the issue. I changed the following in my config:
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://stk-support.ukl.uni-freiburg.de$1 [R=301,L]
The extra slash in the rewrite rule was causing the issue and I also added a HTTPS rewrite condition.

Related

apache rewrite not routing unicode urls

I am using Xampp [Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.5.38].
When I route through alias domain names like domain.dev (set in windows host to redirect to 127.0.0.1) everything is working as expected (urls like domain.dev/سایت are routing correctly) but when I use localhost/site_root/سایت I get 404 not found error.
All the non unicode/utf8 urls are routed correctly; for instance localhost/site_root/path1/path2.
I have already commented line Alias /error/ "D:/xampp/apache/error/" in apache/conf/extra/httpd-multilang-errordoc.conf as commenting this allowed domain access possible.
Here are related config/files:
host:
127.0.0.1 domain.dev
apache/conf/extra/httpd-vhosts.conf:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs/site_root"
ServerName domain.dev
ServerAlias demo.domain.dev
</VirtualHost>
htdocs/site_root/.htaccess:
<FilesMatch "\.(ttf|ttc|otf|eot|woff)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
IndexOptions +Charset=UTF-8
RewriteEngine On
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

htaccess redirect not working on co.uk domain

Here is my apache configuration:
DocumentRoot "/www/public"
<Directory "/www/public">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
NameVirtualHost *:80
<VirtualHost *:80>
ServerAlias www.domain.com domain.com
DocumentRoot /www/public
ServerName www.domain.com
</VirtualHost>
<VirtualHost *:80>
ServerAlias www.domain.co.uk domain.co.uk
DocumentRoot /www/public
ServerName www.domain.co.uk
</VirtualHost>
Here is what my htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !=/server-status
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [NC,L,QSA]
</IfModule>
If I access www.domain.com, domain.com, or www.domain.co.uk everything works fine. However, when I access domain.co.uk, it doesn't redirect properly to www.domain.co.uk. It seems like the co.uk requests are not reading my htaccess.
Solution was to change the HTACCESS from:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
To
RewriteCond %{HTTP_HOST} (^[^.]+\.[^.]+$|^[^.]+\.co\.uk$)
That is due to the regex used in www adding rule. Change that to:
RewriteCond %{HTTP_HOST} ^[^.]+\.(?:[^.]+|co\.uk)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Make sure to clear your browser cache before testing this change.

http to http redirect virtual host in apache

Hi im configuring a server in CentOS 6.5 with apache 2.4 and PHP 5.5
I install an SSL certificate and all go fine.
in another server i did the http to https redirect on htaccess file.
i meke test with that in my new server and didnt work, here are my configuration:
My .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/phpmyadmin/.*
RewriteCond %{REQUEST_URI} !/phpMyAdmin/.*
RewriteRule ^ index.php [L]
</IfModule>
My virtual host file:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName subdomain.domain.com
ServerAlias www.subdomain.domain.com
DocumentRoot /var/www/laravel-project-namel/public
<Directory /var/www/laravel-project-namel/public>
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
NameVirtualHost *:443
<VirtualHost *:443>
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLCertificateFile /etc/pki/tls/certs/cert-valido_clientes_databyte_cl.crt
SSLCertificateKeyFile /etc/pki/tls/certs/cert-valido-private.key
SSLCertificateChainFile /etc/pki/tls/certs/cert-valido_clientes_databyte_cl.pem
ServerName subdomain.domain.com
ServerAlias www.subdomain.domain.com
DocumentRoot /var/www/laravel-project-namel/public
<Directory /var/www/laravel-project-namel/public>
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
</Directory>
</VirtualHost>
i replace the domain and the folder to generic names, but all other are true.
(sorry about my english)
i did search and test every solution i found, but my site refuse to redirect.
Problem is you're not capturing the URI in patter and using $1, %1 as back-reference.
Try this rule in your root .htaccess as very first rule:
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]

How go to home page correctly?

I enable mod_rewrite but when I go to home page the browser try download something. When I go to app_dev.php the browser show error:
The requested URL /app_dev.php/ was not found on this server.
My locale host was configurated to web directory.
the output of 'app/console router:debug _welcome' is:
[router] Route "_welcome"
Name _welcome
Path /
Host ANY
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults _controller: Acme\DemoBundle\Controller\WelcomeController::indexAction
Requirements NO CUSTOM
Options compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex #^/$#s
my httpd-vhosts.conf:
<VirtualHost *:80>
ServerAdmin myemail#email.com
DocumentRoot "/var/www/symfony/hz.dev/web"
ServerName hz.dev
ServerAlias www.hz.dev
ErrorLog "/var/log/apache2/hz.dev-error_log"
CustomLog "/var/log/apache2/hz.dev-access_log" common
</VirtualHost>
My web/.htaccsess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
Try this .htaccess instead. It's very simple, I use it during development.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
You should then be able to access Symfony at http://example.com/ without app_dev.php in the URL

How to redirect from www.domain.com to domain.com?

I am using the following code in my httpd.conf for redirections:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
But, it ends up redirecting to www.domain.com/domain.com//domain.com//domain.com
I want all the following URLs to redirect to domain.com:
http://domain.com
http://www.domain.com
www.domain.com
This should do it. The problem is that it looks pretty much like what you already have. When you type http://domain.com in your browser, does it work? Or does it redirect to somewhere else?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteRule ^(.*)$ http://domain.com$1 [R=permanent,L]
http://www.webweaver.nu/html-tips/web-redirection.shtml suggests
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
# Redirect ANY non-www request to https and www
RewriteCond %{HTTP_HOST} !^(www.\.domain\.com)?$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
# Redirect ANY HTTP request to https and www
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]
for SSL Virtual host use the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com
RewriteRule ^(.*)$ https://yourdomain.com$1 [R=301,L]
Full Example below:
<VirtualHost *:443>
ServerAdmin youname#yourdomain.com
DocumentRoot "/var/www/html/yourdomain"
ServerName yourdomain.com
ServerAlias www.yourdomain.com *.yourdomain.com
ErrorLog "logs/yourdomain-error_log"
CustomLog "logs/yourdomain-access_log" common
ErrorDocument 404 /index.php
<Directory "/var/www/html/yourdomain">
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/yourdomain/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/yourdomain/chain.pem
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com
RewriteRule ^(.*)$ https://yourdomain.com$1 [R=301,L]
</VirtualHost>