.htaccess redirect based on multiple conditions - apache

Im trying to configure a .htaccess RewriteRule and i'm struggling.
I have a domain www.domain.com
I'm trying to add these conditions.
I'm working on www.domain.com/.htaccess
if URL is www.domain.com with no path to file, redirect to www.domain.co.uk
if URL is www.domain.com AND has a path to file, no rewrite, allow.
if URL is www.domain.com AND has a path to file AND file doesnt exist, return 404
if URL points to a directory, return access forbidden
They are my requirments.
I've tried multiple things. Heres my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]
This code satisfies the first constraint.
However i have no idea how to check if a path exists, if the path is valid, or if its a directory.
A perfect answer would be code satisfying all the constraints and explainatoin how it works.
I've read the mod_rewrite documentation more times than I care to admit :'(

# Make sure / redirected to other domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/$ http://www.domain.co.uk [R=301,L]
# Make sure directory listings are forbidden
Options -Indexes
Note that you can use RedirectMatch, which is actually preferred over rewriting in this case
and there is no need to handle files at all in your case - if the URI is not /, then the rules do not apply and regular file handling will be performed (404 error if not found, etc... )

Related

apache apply rewrite rules to only one subdirectory

I have the following directory structure:
/var/www/html
--myapp
--phpmyadmin
In the /var/www/html folder there is a .htaccess file with following content:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.sk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.sk$
RewriteCond %{REQUEST_URI} !myapp/public
RewriteRule (.*) /myapp/public/$1 [L]
If I access the http://mydomain.sk the rewrite rules work as expected and I get the myapp/public/index.php file as root. However, if I want to access http://mydomain.sk/phpmyadmin the request is redirected to http://mydomain/myapp/public/phpmyadmin. Here I get the error 404 - page not found.
I cannot find a way to only serve http://mydomain.sk/myapp/public as a root folder and to serve http://mydomain.sk/phpmyadmin "as is".
It took some time to figure this one out. The rules posted above actually work as expected. It turns out I had another set of rewrite rules in apache2.conf file (that I totally forgot about) that took precedence over my .htaccess file.

Apache mod_rewrite from a folder to a different domain doesn't work with trailing slash

I'm trying to rewrite URLs from old.domain.tld/project to domain.tld/subfolder/project using .htaccess in the project directory on the old server. I think I need to check for the host name so the rule only applies to the old server, even if the .htaccess file is also put on the new server.
I've tried the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/subfolder/project/$1 [R=301,L]
This works fine for URLs like https://old.domain.tld/project/index.php?p=q (redirects to https://domain.tld/subfolder/project/index.php?p=q) but not for the URL without the trailing slash—https://old.domain.tld/project ends up being redirected to https://domain.tld. Very odd! How do I make this work for both types of URL?
I tried a rather different method to make this a generic redirect, that could be included even if the folder name wasn't project, but this has the same problem:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . https://domain.tld/subfolder/%1 [R=301,L]
In a server-wide configuration, a slash is appended to all requests for index files, e.g. old.domain.tld/project is redirected to old.domain.tld/project/. Could this be causing a problem?

redirectMatch in mod rewrite

We recently moved our web site to another server, say http://newdomain.com, while our old site is at say http://olddomain.com
I would like to keep the users directory on the old site accessible as it was before, that is http://oldomain.com/~user, but anything accessible under the root folder point to the new domain. The users directory on the old server are in the /home/user file system. So, I wrote following rules:
RedirectMatch permanent ^/~(.*) /home/~$1
RedirectMatch permanent ^/(.*) http://newdomain.com/$1
while the second rule works flawlessly, the first one still wants to map to the new site as following: http://newdomain.com/~user.
How can I fix the two rules so that anything in /var/www/html on the old site redirects to the new site, but anything under /home does not redirect?
--
Here is the new code based on Ben's solution, which maps the urls for the access to users' home pages correctly, but the browser complains and does not show their sites. The root folder urls redirection works fine.
RewriteEngine on
# RewriteBase / -- I had to comment this as the apache did not like it
Rewriterule ^/~(.*) /~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^/(.*) http://newdomain.com/$1 [R=301,L]
You should use RewriteCond to test if the URI prefix with /home/~, and use RewriteRule to rewrite the URL and make a permanent redirect.
RewriteEngine On
RewriteBase /
RewriteRule ^~(.*) /home/~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^(.*) http://newdomain.com/$1 [R=301,L]
Line 3: if URI prefix with ~{any_characters}, rewrite the URI to /home/~{any_characters}, redirect permanently
Line 5: Test if the URI not prefix with /home/~{any_characters}
Line 6: Then process the rewrite rule, rewrite the URI from {any_characters} to http://newdomain.com/{any_characters}, redirect permanently

having trouble writing htaccess redirects

I'm having trouble writing redirect rules in my website's htaccess file.
Basically, i want to write two rules:
1 - When i write the base URL, like http://www.example.com, i want it to automatically redirect the user to http://www.example.com/someDirectory.
2 - However, when i write http://www.example.com/Admin, i want it to redirect the user to http://www.example.com/Admin.
Here's what i've managed to do so far:
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt http://www.example.pt/MainFolder
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt/Admin http://www.example.pt/Admin
However this does not work... Any idea on how to do this?
Try it like this,
When there is no request for specific file or directory it will redirect you to your directory mention in rule and for the rest it will work without any rule.
Please check.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^ %{HTTP_HOST}/someDirectory [R,L]
After a long research i was able to find a solution to my problem. I'll leave it here, in case anyone's having the same problem:
#Rewrite everything to subfolder
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/MainFolder
RewriteCond %{REQUEST_URI} !^/Admin
Rewriterule ^(.*)$ MainFolder/$1 [L]

Using .htaccess file to redirect to a subdomain

In my root folder, I have a home directory. I'd like all requests for mydomain.com or www.mydomain.com to be forwarded to home.mydomain.com. In my cPanel, I have already set up the sub-domain home.mydomain.com to point to mydomain.com/home.
Here's what I currently have in my .htaccess file:
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteRule ^(.*)$ \/home\/$1 [L]
This successfully forwards mydomain.com and www.mydomain.com to mydomain.com/home or www.mydomain.com/home, respectively. But home.mydomain.com/filename gives me an internal server error, instead of serving the file at mydomain.com/home/filename.
I'm not sure I understand the exact requirements, but it seems you want to rewrite all (www.)mydomain.com requests to the /home directory while your subdomain home.mydomain.com already points to that directory and thus should be exempt from that rewrite directive. If for some reason (www.)mydomain.com can't be set up to point to /home as well, you'd need a Rewrite Condition, something like
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /home/$1 [L]
I also removed the backslashes which should not be needed. You can use the htaccess tester to check rewrite rules easily online.