Redirect all folders to subdomains htaccess - php

I am trying to redirect every folder from domain.com to it's subdomain.
Examples:
domain.com/a -> a.domain.com
domain.com/b -> b.domain.com
domain.com/about -> about.domain.com
So basically, every folder redirected to it's subdomain. For the first example, folder a contains index.html. When I browser to domain.com/a it should redirect the url to a.domain.com but use the index.html file from the a folder.
My current htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteRule ^(/)?$ %1/index.html [L]
What works
For now, if I navigate to test.domain.com, it's working, it's taking the index.html file from the test folder. If I go to domain.com/test it's still working, but I want to disable this. I want to redirect domain.com/test to test.domain.com as well, but still use the index.html files from the test folder.
Another thing that is in the htacces file is to redirect www to non-www.
Any suggestions would be appreciated.
EDIT:
Forgot to mention that I've tried to add this line:
RedirectMatch 301 ^/test/(.*)$ http://test.domain.com/$1
It's doing the redirection from domain.com/test to test.domain.com/index.html, but it's not loading the page right, I get an error (Page isn't redirecting properly).

keep like
RedirectMatch 301 ^{RELATIVE PATH}$ {ABSOLUTE_PATH}

Related

Redirect root domain with https to subfolder with wordpress

I am trying to point the main domain from my host (https://website.com) which right now just goes to public_html to a subfolder in the public_html directory (new.website.com) with the user still seeing https://website.com without changing.
I tried doing what is found in here: https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
And added this code to the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteCond %{REQUEST_URI} !^/new.website.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /new.website.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?website.com$
RewriteRule ^(/)?$ new.website.com/index.php [L]
However 2 things happened incorrectly:
The domain changed to https://new.website.com/404.html
It is giving me a page to proceed with caution since new.website.com does not have a certificate.
(folder name and also a subdomain are both named: new.website.com)
How can i redirect so it still shows https://website.com and go to the wordpress folder?
If I understand your question right, you need to have website.com visible to the public but actually, WordPress is installed on new.website.com
In this case, you need to do Wordpress multisite.
Check: https://codex.wordpress.org/Create_A_Network

Redirect always to index.php

I'm trying to relocate a site into a temporary folder in order to install a CMS in the root. The redirect works but all the internal links in the site now take me back to the index page in the temporary folder. The .htaccess in the root is as follows:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/old/ [R=301,NC]
it's because you're redirecting everything to the new url. You should put something like this:
RewriteRule ^$ http://www.example.com/old [R=301,NC]
RewriteRule ^(.*)$ http://www.example.com/old/$1 [R=301,NC]
PS: The $1 take the arguments in expression
PS2: You should not use code 301 for a temporary redirect, the 302 is more appropriate https://en.wikipedia.org/wiki/HTTP_302

.htaccess - Redirect domain2.com to domain1.com/folder without changing the url

I have two domainnames. Let's say domain1.com and domain2.com.
My main website is running on domain1.com.
I want to run a blog on domain1.com/blog but I want to use domain2.com as the url for this blog.
I currently have this in my .htaccess file:
RewriteCond %{HTTP_HOST} ^domain2.com
RewriteRule ^(.*) http://domain1.com/blog [P]
This works but the url changes to domain2.com/blog when I enter domain2.com and I just want domain2.com without /blog.
I've been searching for over an hour and I can't find the right solution.
Any ideas?
Try this:
RewriteCond %{HTTP_HOST} domain2.com
RewriteRule ^(.*)$ http://domain1.com/blog/$1 [L]
Try and use this in your root .htaccess:
RewriteEngine On
RewriteRule ^$ blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blog/$1
This will take domain2.com/blog and make the resulting URL show domain2.com but still load the content in the /blog folder.
Make sure you clear your cache before testing this.

htaccess make different page acting as home page

If user goes to URL
http://example.com
The server does a 302 Moved and goes to http://example.com/en/home/my-page.html
What I want is if URL is
http://example.com/en/home/my-page.html
The browser should just show http://example.com/en/
I've tried with .htaccess like so:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^en/home/my-page\.html
RewriteRule ^en/ /en/home/my-page\.html [R=301,L]
RewriteRule ^index.php$ en/home/my-page\.html [L,R=301]
but it does nothing. What am I doing wrong?
Your regex patterns are wrong.
You can use these rule in site root .htaccess:
RewriteEngine on
# externally redirect /en/home/my-page.html to /en/
RewriteCond %{THE_REQUEST} \s/+en/home/my-page\.html[?\s/]
RewriteRule ^ /en/ [R=301,L]
# internally rewrite /en/ to /en/home/my-page.html
RewriteRule ^en/?$ en/home/my-page\.html [L,NC]
Don't forget to clear your browser cache before testing this change.
This code will redirect user hitting
http://example.com/en/my-page.html
to
http://example.com/en/
and than will internally redirect thr request to index.php (you can substitute parameter with the directory where index.php is located or just remove it if it is in the main dir)
RewriteRule ^en/my-page\.html$ /en/ [L,R=301]
RewriteRule ^en/ /<dir>/index.php [L]

how to write a rewriteCond in .htaccess to exclude a subdir and its subdirs

I have the following problem. I have a website and a blog in a subdirectory. Both of them are php. I have a .htaccess file in the root folder and another one in the blog folder. I dont' think is relevant, but the blog script is wordpress.
I added a condition in the root .htaccess to skip the requests made for the blog,
rewriteCond %{REQUEST_URI} !^/blog.*
Here it is how it looks. I removed the rest of the file:
Options +FollowSymlinks -MultiViews RewriteEngine on
RewriteBase / RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
// my added line RewriteCond %{REQUEST_URI} !^/blog.*
RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(all)/([^/]+)/?$ story.php?title=$2 [L] RewriteRule ^(all)/?$ ?category=$1 [L]
RewriteRule ^/?$ index.php [L] RewriteRule ^advanced-search/?$ advancedsearch.php [L] ...
The problem I have is related to the blog requests. For example sometimes if I try to open an url it works fine, sometimes the home (root page not the blog) is opened. It seems very strange. I think it is related to the host. When the host is to busy the blog page I request is not found so the request is going to the root .htaccess.
I have 2 questions:
how to write a rule and where to
place it to exclude all the requests
for /blog to be rewritten by the root
.htaccess? the blog requests might
look like http: //test.com/blog,
http: //test.com/blog/,
http: //test.com/blog/title,
http: //test.com/blog/title/,
http: //test.com/blog/category/title
does anyone has any idea what happens? Why when I open a blog page it opens the home root page, and if I refresh the page it goes to the blog post page?
Take a look into the mod_rewrite documentation, specifically the -d flag in RewriteCond.
It should be something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule HERE

Categories