Reputation: 111
I have difficulties to understand how htaccess rewrite / redirect is working. I have been trough already more than 20 article about it but I don't catch the logic and the syntax of how to write rules properly.
I currently have a server with public_html folder We have our main site under the subfolder /site , this is a wordpress site We have other site (also a wordpress site) we use for e-learning under the subfolder /training
I need to make our site (domain.com) to point to /public_html/site while having domain.com/learning to point to public_html/training
I have tried to understad how queries in htaccess are working such as RewriteCond or RewriteRule
Here is my current code:
# Use PHP5.4 Single php.ini as default
AddHandler application/x-httpd-php54s .php
# File modified on Sun Mar 30 01:22:23 2014 by server
# For security reasons, mod_php is not used on this server. Use a php.ini file for php directives
# php_value max_execution_time 90
# BlueHost.com
# .htaccess main domain to subdirectory redirect
# Do not change this line.
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine on
#RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
#RewriteCond %{REQUEST_URI} !^/site/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /site/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ site_en/index.html [L]
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change domain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'site to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/learning/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'site' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /learning/$1
# Change domain.com to be your main domain again.
# Change 'site' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ learning/index.html [L]
Upvotes: 1
Views: 826
Reputation: 1965
The following code will need to be added to the .htaccess file in the public_html folder of your hosting account.
You will need to insert the following code block and make modifications as noted in the (#) comments.
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change domain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'site to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/site/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'site' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /site/$1
# Change domain.com to be your main domain again.
# Change 'site' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ site/index.html [L]
Repeat Same for Learning domain .
Visitors to your Web site will not be able to tell that your main domain is using a subdirectory, they will still see the Web site address as
Note :
Please note that this will not work with some website software. You may also need to modify the $base_url, $live_site or other configuration settings to finish the process.
Since you mentioned that your case is WordPress , don't forget to have a look at this too
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
Update :
RedirectMatch 301 /learning/(.*) /training/$1
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change domain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'site to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/site/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'site' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /site/$1
# Change domain.com to be your main domain again.
# Change 'site' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ site/index.php [L]
Upvotes: 2