Apache non www to www badly re writting arrays on URL - regex

we have the following .htaccess configuration:
RewriteEngine on
allow from all
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com\/" [R=301,L]
Between other things we want to redirect all non-www URLs to the ones with the www.
Everything works fine, except for the pages where the URL is like this:
index?Form%5bplace%5d=Caribbean&Form%5bdestination%5d=Virgin+Islands&Form%5btype%5d=A
When we enter the URL without the www our redirect ends up with the following URL:
index?Form%255bplace%255d=Caribbean&Form%255bdestination%255d=Virgin+Islands&Form%255btype%255d=A
Which gives an 404 error because it is not recognized.
Any idea how to avoid this?

Replace your code with this:
allow from all
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# if a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php [L]
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteRule ^/?$ http://www.domain1.com/ [R=301,L]

What's happening is that the % symbol is getting escaped to %25.
You can avoid this using the NE flag on your rules

Related

Don't rewrite particular subfolder

I'm using the following .htaccess to rewrite all URLs to my index.php file if they're not actual files or folders:
Options +FollowSymLinks
RewriteEngine On
// This bit redirect www.mydomain.com to mydomain.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
// This bit redirects all requests to index.php if a file or directory
// bearing that name doesn't exist
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
I'm now adding an admin section to my website and I would like to not rewrite this particular URL:
mydomain.com/admin
I would like that case to be treated as it would be by default (in this case defaulting to mydomain.com/admin/index.php )
I've attempted to figure this out by myself but regex is still alien to me...
You can use:
Options +FollowSymLinks
RewriteEngine On
// This bit redirect www.mydomain.com to mydomain.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE]
// This bit redirects all requests to index.php if a file or directory
// bearing that name doesn't exist OR it doesn't start with /admin/
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule !^admin/ ./index.php [L,NC]

Redirect does not occur if trailing slash exists

I have the following lines in my .htaccess file in a directory called blog.
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Different things happen when I do the following:
https://staging.example.com/blog/ - redirect does not work
https://staging.example.com/blog - redirect does work
When a trailing slash exists my redirect does not work, why is that the case?
Update
This is the blog/.htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
# Froce HTTP
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
https://staging.example.com/blog/ - redirect does not work
This could be due a presence of blog/.htaccess. If that is true then add this line on top of other rules there as well:
RewriteEngine On
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you have a catalog with name equal to the redirect "path", it has more priority than your rule.
In your case, if you have a "blog" directory under DocumentRoot, redirecting simply fails.
We need more informaction about your structure and other rules.
But I've not been sleeping for a bunch of nights because of existing directory which prevented my rewriting rules.

.htaccess not working only with internet explorer

i have site, that has to rewrite site.ru and www.site.ru to www,site.ru/ru_RU.
I can't access any Apache config files. In htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{HTTP_HOST} ^www.site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's working in firefox or chrome, but in IE i get "this page can't be displayed". Tested on IE10 and IE8 (not compatibility view) on few computers.
If i write some junk in .htacess, i get 500 error in IE. Without .htaccess site loads ok, but i need it to rewrite url. Any ideas how to fix it?
Your flags are all wrong. Modify your rules to this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule !ru_RU /ru_RU [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
Reference: Apache mod_rewrite Introduction
I can't test it now, but here is my guess:
I think http_host ONLY contains the host part, not the uri. So you end up with an endless loop as www.site.ru is will always match again after your rewrite.
You will need another rule that checks if the uri is empty. Like this (untested):
RewriteCond %{REQUEST_URI} ^$
RewriteRule (.*) /ru_RU [QSA]
You might need another condition for the case that the uri contains a slash.

.htaccess in folder, non-www to www. Issues with another RewriteRule

My .htaccess in a folder looks like:
RewriteEngine On
RewriteBase /profile/
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
Basically, if you go to www.mySite.com/profile/Username, my index.php file takes 'Username' as a $_GET variable, and the URL will look clean (www.mySite.com/profile/Username)
However if you go to mySite.com/profile/username (omitting the www), the URL will look like http://www.mySite.com/profile/index.php?username=username
How can I make it so only the www is added without messing the URL up?
Thanks
Ordering of rules does matter in .htaccess.
Try this code instead:
RewriteEngine On
RewriteBase /profile/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?username=$1 [L,QSA]

Htaccess - replace .html with a forward slash

I want to redirect URLs of this form:
/page.html?variable=value&othervar=true&thirdvar=100
To this:
/page/?variable=value&othervar=true&thirdvar=100
So basically I just want to replace the .html in the middle of the URL with a forward slash, but I need to preserve the get string that comes with it. This is what I tried:
RewriteRule ^page.html(.+)$ /page/$1 [L,R=301]
But this doesn't appear to be working for me. I've made similar things work recently but I can't figure out what I'm missing here. Thanks for any input.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]
# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]