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

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.

Related

Subdomain and it's folder rewrite

Let's explain my issues. I have folder which contains subdomain files.
Folder which contains subdomain files is called "subdomain".
My domain is example.com.
My subdomain is sub.example.com.
Issue is that when I type https://sub.example.com it works but same content is loaded when I type https://example.com/subdomain or https://sub.example.com/subdomain
My question is, how to achieve that https://example.com/subdomain(.*) will redirect to https://sub.example.com(.*) and same with https://sub.example.com/subdomain(.*) that will redirect to https://sub.example.com(.*)
Current .htaccess configuration is as follows:
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule (.*) /subdomain/%1%{REQUEST_URI} [L]
You can use this redirect rule as your topmost rule inside your site root .htaccess or in subdomain/.htaccess:
RewriteEngine On
# use THE_REQUEST to detect presence of /subdomain/ in original URL
RewriteCond %{THE_REQUEST} \s/+subdomain/(\S*) [NC]
RewriteRule ^ https://sub.example.com/%1 [L,NE,R=301]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

External and internal redirection using htaccess

I want to convert http://mywebsite.com/folder/file.html to http://mywebsite.com/file.
I am not using regular expression as first I am only concerned about this one URL only.
I tried this-
Redirect 301 /folder/file.html http://mywebsite.com/file
Using this I am able to externally redirect this URL to the desired one but since the URL doesn't exist so I am getting 404.
Now, in order to internally redirect the new URL to the old one, I am using below command but it doesn't seem to work-
RewriteRule http://mywebsite.com/file http://mywebsite.com/folder/file.html [L]
Use only mod_rewrite directives and use THE_REQUEST variable for external redirection.
# turn on rewrite engine
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+/folder/file\.html[\s?] [NC]
RewriteRule ^ /file [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^file/?$ /folder/file.html [L,NC]

htaccess redirect without the first folder

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]

Strange behavior of htaccess redirect

My goal is to redirect an entire domain to another. Every URL of the old domain shall redirect to the root URL of the new domain.
To achieve this I'am doing:
redirect 301 / http://www.google.de/
Problem is that when I test it on http://localhost/randomPath then it redirects to http://www.google.de/randomPath, but not to the root URL of google.de.
Does anyone know how to solve this?
Use mod_rewrite for finer control on rules like matching Host name etc.:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.google.de/? [L,R]
? in the end of target URI will strip off any existing query string.

Replace parts of the URL with mod_rewrite

I need a mod_rewrite rule to redirect url depending on the hostname they are comming from.
The situation:
We have multiple domains pointing to a same webspace and we need to restrict what the specific host can see/download.
domainname.com/images/logo.jpg and /www.domainname.com/images/logo.jpg should transform into domainname.com/domainname_com/images/logo.jpg
So basically I need a rule/function that replaces the dots in the %{HTTP_HOST} with _ and removes/replaces the www subdomain.
Is there any way to do this with mod_rewrite?
Try these rules:
RewriteCond %{ENV:DOMAIN_DIR} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%2]
RewriteCond %{ENV:DOMAIN_DIR} ^([^.]*)\.(.+)
RewriteRule ^images/.+ - [E=DOMAIN_DIR:%1_%2,N]
RewriteCond %{ENV:DOMAIN_DIR} ^[^.]+$
RewriteRule ^images/.+ %{ENV:DOMAIN_DIR}/$0 [L]
The first rule will take the host and store it without www. in the environment variable DOMAIN_DIR. The second rule will replace one dot at a time; the N flag allows to restart the rewriting process without incrementing the internal recursion counter. Finally, the third rule will rewrite the request to the corresponding directory.