Don't rewrite particular subfolder - regex

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]

Related

htaccess redirect specific TLD to subfolder while preserving existing root-to-subfolder redirect

I have a domain example.com which hosts an English version of a website on example.com/en-US/ and a French version on example.com/fr-FR/
Until now we have used the English version as default, so we would redirect everybody requesting example.com to example.com/en-US/ using these rules in the .htaccess file:
Redirect 301 /index.html http://examle.com/en/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
So when you request http://exmaple.com in your browser, the server will request the index.html and that will trigger the Rediret to the /en-US/ directory.
Now we got the French example.fr TLD - pointing to the same webspace. I am now looking for a way to reirect the browser request http://example.fr to http://example.com/fr-FR/
I have tried adding the following condition, but it results in some kind of loop adding lots of /en-US/s into the URL.
Please help me, I need a way to redirect
hxxp://example.com --> hxxp://example.com/en-US/
hxxp://example.fr --> hxxp://example.com/fr-FR/
and all people explicitly requesting hxxp://example.com/index.html --> hxxp://example.com/en-US/ too
You can use this rule based on condition based on HTTP_HOST:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?:index\.html)?$ /en-US/ [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.fr$ [NC]
RewriteRule ^(?:index\.html)?$ /fr-FR/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L,NE]

Redirect subdomain to a specific folder only without the main domain

I have links like this one:
a.site.co.il
b.site.co.il
...
I configured the server to work with wild cards and redirect these subdomains into a folder on this IP:
62.90.154.000/goin
Then, I created a htaccess on 62.90.154.000 root so using of subdomain will redirect into a specific folder.
An example of this would be:
a.site.co.il => 62.90.154.000/goin
But the Ip will not redirect into goin folder, but to the IP itself:
62.90.154.000 => 62.90.154.000
Here is my htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)$ /$1/ [R=301,L]
RewriteRule ^(!62?|.*)$ goin/index.php?subdomain=$1 [L]
This seems to work, but the problem is that this htaccess also redicreds the main domain (62.90.154.000) into the subdomains' folder!
How can I solve this?!
This rule:
RewriteRule ^(!62?|.*)$
Is just skipping REQUEST_URI that start with /62. It is not skipping your IP address.
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# skip below rule for IP addresses that start with 62.
RewriteCond %{REMOTE_ADDR} ^62\. [OR]
RewriteCond %{HTTP_HOST} ^62\.
RewriteRule ^ - [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.site\.co\.il$ [NC]
RewriteRule .* /goin/$0 [L,NE]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ goin/index.php?subdomain=$1 [L,QSA]
Make sure to completely clear your browser cache before testing this change.

prevent access to a subfolder in a domain after a redirect

Assuming i have the following "mydomain.com" that has is pointed to the folder "public_html".
I wanted to redirect all mydomain.com to mydomain.com/prod/public folder so i edited the .htaccess file as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/prod/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /prod/public/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ prod/public/index.php [L]
the changes work as expected, YAY!!!
how do i prevent access to the site via the following request:
http://mydomain.com/prod/public/
or
http://mydomain.com/prod/public/index.php
Place this rule as first rule in your prod/public/.htaccess file:
RewriteEngine on
# direct access forbidden
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+prod/public(/index\.php|/?)?[\s?] [NC]
RewriteRule ^ - [F]

.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]

Apache non www to www badly re writting arrays on URL

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