Redirect root domain to subfolder - .htaccess

Here is our current .htaccess file with the rules we need to keep but also we need to add a new rule that redirects from the root domain to a subfolder URL
example.com -> example.com/fl/en.html..
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.nz$
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/home.html|/info.html|/flash|/external)
RewriteRule (.*) http://example.com/fl/en.html [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

There doesn't appear to be anything particularly special required, providing you put the directive in the correct place. To redirect the document root (for example.com) to /fl/en.html in .htaccess can be done like so:
RewriteRule ^$ /fl/en.html [R,L]
This just needs to go after the directive that rewrites everything for the host domain.co.nz and before your front-controller. (You could potentially combine this with your existing directive that redirects /home.html, /info.html, etc.)
Your existing rules could be further optimised. Instead of checking the requested URL-path in a RewriteCond directive and allowing everything through in the RewriteRule pattern, it is more efficient to do what you can in the RewriteRule pattern first (since this is what is processed first).
Also, since you are using WordPress, any custom directives you add to .htaccess should be outside of the # BEGIN WordPress section. WordPress itself maintains this section, so any manual customisations you make could be overwritten during an update.
Also, there is no need to repeat the RewriteEngine directive. (The last instance of this directive wins and controls the entire file.)
So, bringing all this together, we have something like:
# Rewrite all requests for domain.co.nz to subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.nz$
RewriteRule !^subfolder /subfolder%{REQUEST_URI} [L]
# Redirect document root to /fl/en.html
RewriteRule ^$ /fl/en.html [R,L]
# Redirect specific paths to /fl/en.html
RewriteRule ^(/home.html|/info.html|/flash|/external) /fl/en.html [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Note that the above directive (with a single R) is a temporary (302) redirect. Change this to R=301 if this is intended to be permanent, but only once you have tested that it's working OK (to avoid caching issues).
As always, make sure you've cleared your browser cache before testing.

Related

Redirect old domain to new domain... all pages

I have two domains pointed to the same directory on my hosting account. I want to use .htaccess to redirect the old one to the new one. This is what I used:
RewriteCond %{HTTP_HOST} ^shavara.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.shavara.com [NC]
RewriteRule ^(.*)$ https://www.shavararcm.com/$1 [L,R=301,NC]
The entire code is:
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^shavara.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.shavara.com [NC]
RewriteRule ^(.*)$ https://www.shavararcm.com/$1 [L,R=301,NC]
</IfModule>
If I type shavara.com into the browser it goes to shavararcm.com but if I type:
https://www.shavara.com/industry-solutions/iridium-suite-for-medical-oncology-billing
it doesn't go to:
https://www.shavararcm.com/industry-solutions/iridium-suite-for-medical-oncology-billing
Any ideas what I am doing wrong?
Move your code above WordPress' default code.
This is because WordPress will already do a rewrite for paths which don't exist as real files, and it specifies the [L] modifier which means "last". So your rewrite will be ignored if the previous rewrite matches.
In case of your code it is fine that it has [L] because it also has [R=301] which causes a "real" redirect anyway, so WordPress' rewrites should happen on the following request and not the current one.

Force WWW when URL contains path using .htaccess

I'm having a problem with my URL and my sessions.
I wish to have ALL website pages be forced to use www. As it looks like now, the website looks like this:
www.example.com into www.example.com
example.com into www.example.com
www.example.com/example/ into www.example.com/example/
example.com/example into example.com/example (this is what's wrong)
This is what my .htaccess file looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^wewent\.net
RewriteRule ^(.*)$ http://www.wewent.net/$1 [L,R=301,NC]
</IfModule>
# END WordPress
Because the URL does not redirect properly I get double up with sessions one for www and one for the website without. How can I prevent this the best way?
It seems to look ok but one thing you should do is always put your other rules before the wordpress rules as a habit. When using wordpress it should generally be the last set of rules since it does all the routing. Now for the redirect, you should probably use 302temporary which will remove any current cache and verify that your redirects are working properly. Then you can change it to 301 for permanent once it's working correctly.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.wewent\.net [NC]
RewriteRule ^(.*)$ http://www.wewent.net/$1 [L,R=302,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect folder only using htaccess, not pages in folder

I have a link structure dynamically generated based on this:
http://www.website.com/profiles/
This page shows a listing of all profiles.
For filtering reasons, we also have
http://www.website.com/profile-category/categoryA/
This would only list the profiles of category A. However, when one removes the '/categoryA/' from this url, they get a 404 page not found.
I would like to redirect http://www.website.com/profile-category/ without http://www.website.com/profile-category/categoryA/ being affected. Is this possible using .htaccess?
I know the directory redirect in .htaccess, but this also affects all underlying pages.
Existing rewrite rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
To ensure nothing following the bare directory path is matched, terminate it with a $ and optionally allow a trailing slash with /?. Order is very important with rewrite rules, so you must place this before WordPress's final catch-all rule.
First, test placing it before any of WordPress' rules. Failing that, insert it between them.
RewriteEngine On
# Redirect profile-category with nothing following to /profiles list
RewriteRule ^profile-category/?$ /profiles [L,R=301]
# Then WordPress
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I left WP's full block above because I'm not sure if it is dynamically written by the application (which could break your rule occasionally). A more logical arrangement for the entire set of rules would be:
RewriteEngine On
RewriteBase /
# Never modify requests to index.php
RewriteRule ^index\.php$ - [L]
# Redirect profile-category with nothing following to /profiles list
RewriteRule ^profile-category/?$ /profiles [L,R=301]
# Wordpress - write all requests to non-existing files & dirs
# into index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

.htaccess, Redirect Non-Existing Except a Few URL Rewrites

I have a webpage where if non-existing files or folders were to be typed by a user http://wwww.somewebsite.com/nonexistingfolder or http://www.somewebsite.com/nonexistingfile.html will just simply forward to www.somewebsite.com. Then would like to create a web page from www.somewebsite.com/about.html to www.somewebsite.com/about since shorter is better in my opinion. Using .htaccess I thought I could use RewriteCond for non-existings and RewriteRule for the user-friendly like web page url. I'm terrible at .htaccess I only know basics, I did my research even the questions that may have been asked already but not sure how to write this exception rule.
How can I add a code in .htaccess so that I can have all non-existing files/folders except the web page url I specified?. This one below will simply redirect all non-existings to index.html and when I do www.somewebsite.com/about (from /about.html) simply goes to index.html. Any help?
--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
# Rewrite existing files sample RewriteRule ^contact/$ /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/$ index.html [L]
RewriteRule ^about/$ about.html [L]
RewriteRule ^services/$ services.html [L]
RewriteRule ^application/$ application.html [L]
RewriteRule ^contact/$ contact.html [L]
RewriteRule ^privacy/$ privacy.html [L]
You need to do the all-or-nothing rewrite (e.g. the RewriteRule ^(.*)$ / [L,QSA]) after all of your other more specific rules. The rules all get applied in the order that they appear in, so this means your first rule simply always gets applied, and then the rewrite engine stops.
Swap the order around and try again:
# Rewrite existing files sample RewriteRule ^contact/$ /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/?$ index.html [L]
RewriteRule ^about/$ about.html [L]
RewriteRule ^services/$ services.html [L]
RewriteRule ^application/$ application.html [L]
RewriteRule ^contact/$ contact.html [L]
RewriteRule ^privacy/$ privacy.html [L]
--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
Also this rule:
RewriteRule ^/$ index.html [L]
Will never get applied because leading slashes are removed from the URI before applying rules to them, so ^/$ will never match, you want ^$ or ^/?$ instead.

I need a Wordpress folder to NOT be case sensitive without writing 200 redirects

I have a blog set up at blog.ftj.com/ACSM, it is hosted with Bluehost and their folder structures seem to be case sensitive. Is there something in the .htaccess file that I can adjust so that all possible combinations get redirected to the specific uppercase URL.
Another issue is that it seems that I need to redirect
blog.ftj.com/acsm/
with and without the forward slash.
Here is my current .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ACSM/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ACSM/index.php [L]
</IfModule>
# END WordPress
Please submit the full change if you would.
You need to place the following .htaccess in the root dir to rewrite all requests to /ACSM into /acsm
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/acsm$ [NC]
RewriteRule ^(.*)$ /ACSM [L]
RewriteCond %{REQUEST_URI} ^/acsm/(.*)$ [NC]
RewriteRule ^acsm/(.*)$ /ACSM/$1 [L]
</IfModule>
Sorry for delays, have not got an Apache at hands....

Resources