apache rewrite not routing unicode urls - apache

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]

Related

Apache vhost double slash on redirects

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.

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]

Combining Rewrite Rules - http to https AND all links to one file while maintaining clean URL

Over the past few days, I have read quite a few forums and StackOverflow answers on rewriting URLs in apache.conf and converting http to https using rewrites. Thanks to StackOverflow, I have figured out how to do both separately. Unfortunately, I haven't found a solution that can combine the two while allowing me to keep my clean URL appearance AND while letting me pass a variable to page in a hidden manner.
Here is the current directory part of my apache.conf file:
<Directory /home/tim/example>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1
</Directory>
I currently pass of "https://example.com/CA/Los-Angeles" to a file called "sort-url.php", where I get the "rt" variable, explode it into an array using the "/", and then use the variables like $route_array[0] for "CA" and $route_array[1] = "Los-Angeles". Right now, the user will never see "sort-url.php", so my URLs always look clean. I want to keep this same effect, but convert http to https for all sites.
I found this code to convert to https if I substitute this rewrite rule for the other rewrite rule:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}/sort-url.php?rt=$1 [R,L]
But, this causes "sort-url.php?rt=CA/Los-Angeles" to show up in the URL. If I get rid of the "sort-url.php in the rewrite rule like this:
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
It won't know to route all requests to the sort-url.php file, but it will look like the clean link I want with https.
Am I going about it the wrong way? Is it possible to combine the two rules while maintaining a clean URL?
Thank you in advance,
Tim
ADDITION - Here are my virtual hosts:
<VirtualHost XXX.XXX.XX.XXX:12345>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:443>
ServerName example.com
DocumentRoot /home/tim/example
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/certs/example.key
SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt
</VirtualHost>
Enable .htaccess if not already enabled.
Try this code in your DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1 [L,QSA]
I suggest suppressing Directory clause (COMMENT), and this in vhosts:
<VirtualHost XXX.XXX.XX.XXX:80>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /home/tim/example
Redirect / https://example.com/
</VirtualHost>
<VirtualHost XXX.XXX.XX.XXX:443>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /home/tim/example
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/certs/example.key
SSLCertificateChainFile /etc/ssl/certs/sf_bundle.crt
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /sort-url.php?rt=$1 [L,QSA]
</VirtualHost>
Ok, it appears my load balancer configuration (SSL termination on LB, port 80, etc.) was throwing me for a loop and "X-Forwarded-Proto" was the answer I needed. Here is my updated directory part:
<Directory /home/tim/example>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sort-url.php?rt=$1 [L,QSA]
</Directory>

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