htaccess redirect without the first folder - apache

So we have a system in place for dynamic content for clients.
Essentially they have their own domain. But we also store the content on our domain for deployment.
An example:
Our domain: http://domain_one.com/client_domain/home.php
Their domain: http://client_domain.com/home.php
We need to redirect to the client domain which we can place into the htaccess with php.
What we want to do is also redirect the query string. When we add the query string, it redirects the client_domain/home.php to the client_domain.com
Our rewrite url as follows:
RewriteRule !^(template_files)($!|/) http://".$domain."/ [R=301,L]
This file gets created dynamically via php for those asking about the ".$domain." bit.

You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/(template_files|web_images)/ [NC]
RewriteRule ^[^/]+/(.*)$ http://domain.com/$1 [R=301,NC,L]

Related

How to get domain name from URL and redirect to respective folder using rewrite rule

In my application when the url hit like
http://monohar.opens.com
it should redirect to
http://xx.xxx.xx/folder/manohar
Using htaccess rewrite rule , here domain 'monohar' will be dynamic domain name , could be changed to any thing dynamically but url always ends with 'opens.co'
Please suggest me how to achieve this
You can use this rule in your site root .htaccess or Apache config:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.opens\.com$ [NC]
RewriteRule ^ http://xx.xx.xx/folder/%1%{REQUEST_URI} [L,NE,R=301]
%1 is back-reference of substring before first DOT in HTTP_HOST variable.

Dynamically redirect to subfolder using .htaccess, hide subfolder in url without using domain name

I have a subfolder I want to redirect to, I want it to redirect dynamically so anytime I go to:
example.domain/about it takes me to example.domain/subfolder/about, but still shows: example.domain/aboutin the address bar
However, I don't want to have to specify a domain name so I can use it in any project. So I suppose just automatically want to declare the root domain name as a variable?
I have this working here:
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L,R=301]
And it doesn't require the domain name, but it still shows subfolder in the domain name.
The strategy in this answer is what I need, but minus the domain name:
RewriteEngine On
RewriteRule ^/subfolder/(.*)$ http://www.example.domain/$1 [L,R=301]
I also need it to work for both the www. and the naked domain.
Try the following rule :
RewriteEngine On
#remov php exten
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1.php -f
RewriteRule ^(.*)$ /subfolder%{REQUEST_URI}.php [NC,L]
#redirect root to sub
RewriteRule ^((?!subfolder).*) /subfolder%{REQUEST_URI} [L,NC]

remove beginning forward-slash on htaccess RewriteRule

The forward-slash in my htaccess file is having problems. I need this htaccess to work on multiple servers with different domain names.
I am trying to use the %{HTTP_HOST} parameter to make the domain portion of the rewriterule dynamic. It is working for the most part, except that the domain name is getting '/' put in front of domain.com
example:
/domain.com/folder/detail
How can I remove that first '/'? Here is my code:
RewriteCond %{REQUEST_URI} ^/folder/([^\.]+)
RewriteRule ^([^\.]+)/([^\.]+)$ %{HTTP_HOST}/$1.php/$2 [NC,L]
I am not trying to redirect to another domain, this htaccess file will be with a project that will need to run on multiple servers(local and remote). They all have different domain names to them. so htaccess needs to be dynamic about it.
On one remote server it is trying to route from the very root of the document not going through the http_host like http://domain.com for example. So this is why I am putting in %{HTTP_HOST}.
The problem is rewriterule is prepending a '/' to the rewrite automatically.
Thanks a lot
Change it to:
RewriteCond %{REQUEST_URI} ^/folder/([^\.]+)
RewriteRule ^([^\.]+)/([^\.]+)$ http://%{HTTP_HOST}/$1.php/$2 [NC,L]

Using .htaccess to redirect from one folder to another, if domain matches

I need to rewrite all URLs from one folder to another folder, but only if they are doing a request from a certain domain.
So:
www.example.com/blog/path/to/article
needs to go to:
www.example.com/new-blog/path/to/article
But it needs to be conditional to example.com, because if the request comes through for, say:
www.otherdomain.com/blog/path/to/article
it does not need to redirect to the new folder.
Both the folders "blog" and "new-blog" contain two separate Wordpress installs. This is the code I currently have in the root public folder, and what does not work:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^blog(.*) /new-blog$1 [L,R]
Any help is appreciated. Thanks.
I was able to get it working with this directive, and actually placed inside the /blog folder:
RewriteCond %{HTTP_HOST} ^new\.example\.com$ [NC]
RewriteRule (.*) http://new.example.com/our-happy-place/$1 [L,R]

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

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.