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

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]

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.

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]

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]

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]

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]