apache .htaccess file https:// redirect ---- rewrite it for subdirectory? - apache

here is my code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This will successfully rewrite everything to https (it will "force" https), if I put it in my home directory's .htaccess file. However, what about if I only want my subdirectory of /support to force https? How to rewrite the above code?
The intent is with regards to forcing https in WHMCS
Thanks in advance!

If you want to redirect only a subfolder and not a subdomain, that is even easier. To redirect the subfolder /support/ you would use the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/support/(.*)$ https://%{HTTP_HOST}/support/$1 [QSA]
I added a QSA flag, which will force any query string that was entered to be appended to the redirect URL. If you don't use query strings in this section of the site, you can remove that flag. Also, if the string as shown doesn't work, you can try removing the leading slash from the RewriteRule and retrying it.

Add the following rule before the %{HTTPS} line:
RewriteCond %{HTTP_HOST} ^support.domain.com$
This will only allow the rule to execute if the visitor is coming from the subdomain. Please note, these rules will likely need to be put in the .htaccess file that is the root for the subdomain, not the main site root folder.

Related

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?

apache redirect to remove a subfolder from the url

I have issues redirecting a specific url within the same domain
Example, I want to redirect
http://example.tld/folder/*
to
http://example.tld/*
where * is a wildcard
Basically you will need this type of code in your .htaccess file:
RewriteEngine On
RewriteRule ^/folder/ http://domain.tld/? [R=301,L]
This should redirect your current url http://domain.tld/folder to http:/domain.tld/
Or:
RewriteEngine On
RewriteRule ^folder/(.*)$ /$1 [R=301,NC,L]
Please try and see which one works best for you

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]

redirect based on directory in path via .htaccess

Via .htaccess, how do you redirect this
http://www.somedomain.com/de/foo
to this:
http://www.de-domain.com/foo
The redirection should depend on the second parameter, in the example above "de".
Depending on the server configuration you should be able to do this on the .htaccess for somedomain.com. If this works appropriately you might try [R=301,L] to make the redirection permanent:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?somedomain.com$
RewriteRule ^([a-z]{2})/(.*) http://www.$1-domain.com/$2 [R,L]

.htaccess mod_rewrite redirect without query string variables not working

I'm having a problem with one of my rewrite rules. I would like to redirect all of the following URL's to another URL without the query string.
/gallery/products.aspx?C=9&SC=&ID=428&P=10
/gallery/products.aspx?C=2&SC=2&ID=128&P=1
/gallery/products.aspx?ID=147&C=2&SC=&P=7
/gallery/products.aspx?ID=1337&C=15&SC=&P=1
/gallery/products.aspx?ID=1532&C=3&SC=&P=2
/gallery/products.aspx?C=9&SC=&ID=1489&P=1
/gallery/products.aspx?C=7&SC=&ID=100&P=2
/gallery/products.aspx?C=2
/gallery/products.aspx?ID=1328&C=14&SC=11&P=17
/gallery/products.aspx?C=1&SC=&ID=767&P=3
/gallery/products.aspx?ID=1270&C=1&SC=&P=26
and I have this in my .htaccess file
RewriteRule ^gallery/products.aspx http://www.domain.com/category/? [L,R=301]
but it's not working. I checked it in a .htaccess simulator and it found the rule then redirected, but when I upload to my server, it doesn't redirect. I've also tried some other rules with no luck
I was finally able to make this work with the following:
RewriteCond %{HTTP_HOST} www.domain.com [NC]
RewriteRule products.aspx http://www.domain.com/category? [L,R=301]