wordpress .htaccess www to nonwww (not working) - wordpress

I have a WordPress website. The permalink is changed so that a .htaccess file is created
I have one more requirement which is to redirect www to nonwww
for example I entered https://www.why-deny.com.hk/
it still didn't redirect to https://why-deny.com.hk/
After editing the .htaccess, www still unable direct to nonwww.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.why\-deny.com.hk [NC]
RewriteCond https://www.why-deny.com.hk/ [NC]
RewriteRule ^(.*)$ http://why-deny.com.hk/$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
A record is added
httpd.conf virtual host are added
<VirtualHost *:80>
ServerName www.why-deny.com.hk
Redirect permanent / https://why-deny.com.hk/
</VirtualHost>
<VirtualHost *:443>
ServerName www.why-deny.com.hk
Redirect permanent / https://why-deny.com.hk/
</VirtualHost>

RewriteEngine On
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.why\-deny.com.hk/ [NC]
RewriteRule ^(.*)$ https://why-deny.com.hk/$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

Please add this one before # BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Related

Subdomain route to subfolder with htaccess on wordpress

I am trying to create a subdomain sub.domain.com that displays domain.com/sub without changing the address bar.
I've come up with the following .htaccess rule based on other questions made here:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [L,NC]
The problem is I'm using a WordPress instance and my .htaccess currently looks like this now:
# 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
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [L,NC]
But when I browse sub.domain.com I'm being redirected to my homepage (domain.com) from WordPress. How can I do this correctly?
Tried to change it to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [NC]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
But it is still not working
You have to create a virtual host in apache config.
<VirtualHost *:80>
ServerName sub.domain.com
DocumentRoot /var/www/sub
</VirtualHost>
Or VirtualDocumentRoot if mod_vhosts_alias is enabled.
<VirtualHost *:80>
ServerName sub.domain.com
ServerAlias *.domain.com
VirtualDocumentRoot /var/www/%1
</VirtualHost>
In above I am pointing it to /sub directory if it exists. After all changes restart the server.
You can add below line of code in your wp-config.php files
define('WP_HOME','http://sub.domain.com');
define('WP_SITEURL','http://sub.domain.com');

My https website is redirecting to my old http website

a while ago i made a copy of my wordpress website and restore it on a ssl/https verficated account and since that, my new site have been redirecting to http.
I've tried with the htaccess code and it doesn´t work. I'll paste my code below:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://voladurascontroladas.com/$1 [R,L]
# BEGIN rlrssslReallySimpleSSL rsssl_version[2.5.20]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTPS_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
# 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

htaccess, directadmin vhost and wordpress

i have task to create connect to wordpress by default https with wwww in url
#Require SSL
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# 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 this configuration dont working. Maybe httpd.conf or directadmin.confg have some flag/configuration for this?
Thanks.

htaccess rewrite rule for sub domain 301 redirect - WordPress set up

I have been looking into this and had a question for a 301 redirect. I am using WordPress and used to have about 5 different sites using subdomains that are now just going to be one site at the root level.
So basically I want sub.yourdomain.com, sub2.yourdomain.com and sub3.yourdomain.com to redirect permanently to yourdomain.com. Wordpress already has a rewrite rule so was not sure if I add my 301's to that or create a new rule.
Here is what I have:
# 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
# BEGIN SEO Redirects
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.yourdomain.com$
RewriteCond %{HTTP_HOST} ^sub2.yourdomain.com$
RewriteCond %{HTTP_HOST} ^sub3.yourdomain.com$
RewriteRule ^(.*)$ http://yourdomain.com/ [R=301,L]
# END SEO Redirects
You can keep this rule before default WP rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(sub|sub2|sub3)\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/ [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

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.

Resources