redirect address to directory htaccess - wordpress

Pretty straight forward, I have an address www.example.com, and I need to redirect it to a subfolder which the actual website resides in.
I figured out how to redirect a http protocol request to https but this one I couldn't from what I've read.
This is my htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/subfolder/ [R=301,NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress
AddHandler application/x-httpd-php55 .php .php5 .php4 .php3

Use a Negative lookahead in regex like:
RewriteRule ^(?!subfolder/)(.*) http://www.example.com/subfolder/$1 [R=301,NC,L]

Related

301 Redirect for All Subdirectories

I am changing domain names for a WordPress site and want to set up a structure like so:
If a user accesses olddomain.com/page, he will be re-directed to newdomain.com/page, etc. In other words, only the pre-slash part of the domain would change. I want to do this for all pages under the old domain.
So far, I have only been able to get olddomain.com to re-direct to newdomain.com, but not any of the sub-directories (for example, olddomain.com/page does not re-direct to newdomain.com/page). Here is my .htaccess file:
# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^newdomain\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL
# 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
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
Does anyone know what I did wrong?
You must put all of the redirect rules before your routing rules. The wordpress rules route everything to index.php, including anything you intend to redirect. So the redirects must happen before any sort of internal routing happens.
Simply put your redirect rule before the wordpress rules:
# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^newdomain\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
# 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
Change your .htaccess to the below code on the old domain:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
</IfModule>
This will redirect the entire website on a page by page basis.

How to move WordPress form HTTP to HTTPS

I have following code in my .htaccess file of a wp site
# 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 have done all the thing need for a WordPress site to operate in HTTPS now my site is opening in both the format i.e in HTTP and HTTPS, but if the user type example.com it goes to http not in https so to solve this I add few more code to solve this issue
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]
# 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
but after adding this my site in not opening it says "ERR_TOO_MANY_REDIRECTS"
How to solve this issue, I want the user to go directly to https
The problem is that you're not checking if https is being used, so it just constantly tells the browser to go to https://www.yoursite.com/$1.
You can use %{HTTPS} to check whether the request uses https.
RewriteEngine on
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]

Redirect all Wordpress pages to SSL apart from one

I want to redirect all pages on my Wordpress website to SSL apart from one.
I have tried numerous ways to do this and have succeessfully got the SSL working, but I cannot get the single page exclusion to work.
I am assuming the problem lies with the Wordpress specific rewrites.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_URI} ^parent-page/child-page/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
# END WordPress
Any suggestions?
You always need your rules that redirect to be before the rules that internally rewrite. Otherwise, internal stuff (like /index.php) end up getting redirected.
Try:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/parent-page/child-page/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_URI} ^/parent-page/child-page/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Note that you also need to add a / leading slash to your %{REQUEST_URI} conditions, those will always start with a /. Additionally, you need to not redirect to https with that URI, so you need to add a negative exclusion condition to the redirect to https.

wordpress http to https redirect loop

I am trying to force my site to load in HTTPS. I can create a redirect in PHP which works for PHP files, but I want all requests forced to HTTPS - JS, CSS, Images, etc.
Here's what I have in my .htaccess:
ErrorDocument 401 default
# 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
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I have tried various methods of this by placing the redirect in different locations and such, but everything seems to make this into a redirect loop. Any ideas what I could do to fix that?
Thank you.
Keep your http to https rules before internal WP rule:
ErrorDocument 401 default
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Make sure WP's home URL and site URL also have https in permalink settings.
Set a 301 permanent redirect which is more SEO friendly. Already tested:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect all traffic to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Wordpress rules
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect single directory to HTTPS

I want to redirect directory /api/ and everything after it to HTTPS. Everything else must redirect to HTTP if a visitor tries to visit something with HTTPS.
I'd like WordPress to ignore the /api/ directory (and everything after it) from the current mod rewrite.
This is my current standard WordPress .htaccess:
# 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
Have you tried adding :
RewriteCond %{REQUEST_URI} YourFolderName
RewriteRule ^(.*)$ https://www.example.com/YourFolderName /$1 [R,L]
(Taken from HERE for more info)
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources