Subdomain route to subfolder with htaccess on wordpress - 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');

Related

wordpress .htaccess www to nonwww (not working)

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]

Links on virtualhost with Xampp not working

I'm trying to set up a virtualhost on my machine for a wordpress project
I added this to the http-vhosts.conf in Apache
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\wordpresstest"
ServerName myapplication.dev
<Directory "C:\xampp\htdocs\wordpresstest">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I added this line to the windows hosts file
127.0.0.1 myapplication.dev
And I added these lines in wp-config
define('WP_HOME','http://myapplication.dev');
define('WP_SITEURL','http://myapplication.dev');
I also added a plugin to allow relative URLs across wordpress
However, when I access http://myapplication.dev, the first page of my website is there, but none of the links work ! They all send a 500 error page
Ok, found the solution thanks to this link : https://gist.github.com/JustThomas/141ebe0764d43188d4f2
I changed my .htaccess file
Before :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpressTest/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpressTest/index.php [L]
</IfModule>
# END WordPress
After :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

www. redirect is failing with .htaccess

I have a wordpress site running on Ubuntu with apache2 server.
I want the server to redirect all traffic from www.mysite.com to mysite.com.
I made the modification in .htaccess file to look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#RewriteEngine On
#RewriteBase /
#RewriteRule ^index\.php$ - [L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule . /index.php [L]
</IfModule>
And I modified apache2.conf to this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Still no success.I still get the Ubuntu apache server default page for www.mysite.com. What am I missing?

htaccess external domain to new domain

I have asked my domain host provider to point a website to my public_html folder. but now I have been told the if I need this other domain to redirect to a particular url on my primary domain website, I would need to do it in the htaccess file. At the moment I have researched and this is what my htaccess looks like without any luck!
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 5
DirectoryIndex index.cgi index.php
# 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} ^otherdomain.org.uk
RewriteRule ^(.*)$ http://www.primarydomain.org/blog/$1 [R=permanent,L]
The above does not redirect my otherdomain.org.uk to the blog page on my primarydomain.org/blog. Please note that my primarydomain.org/blog is a wordpress website page.
Keep redirect rules before your WP rules.
Have your rules like this:
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 5
DirectoryIndex index.cgi index.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^otherdomain.org.uk
RewriteRule ^(.*)$ http://www.primarydomain.org/blog/$1 [R=permanent,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect of www to non-www works works for all URLs except the root

I have the following code in my Apache htaccess file that is used to redirect all www URLs to non-www URLs on wordpress. This works for all URLs except the root.
For example, http://www.noshon.it/recipes redirects properly to http://noshon.it/recipes. However, http://www.noshon.it incorrectly redirects to http://noshon.it/wordpress (where it receives a 404 page).
I can't figure out why all ULRs work except for the root. Any help would be much appreciated.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.noshon\.it$ [NC]
RewriteRule ^(.*)$ http://noshon.it/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Thanks to #WebChemist for suggesting that the redirect was happening in another file. I located a file named wordpress.conf in the /opt/bitnami/apps/wordpress/conf/ directory of the Bitnami EC2 instance. This file had the following code:
# App url moved to root
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
#Alias /wordpress/ "/opt/bitnami/apps/wordpress/htdocs/"
#Alias /wordpress "/opt/bitnami/apps/wordpress/htdocs"
<Directory "/opt/bitnami/apps/wordpress/htdocs">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
#RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<//Directory>
# Uncomment the following lines to see your application in the root
# of your URL. This is not compatible with more than one application.
RewriteEngine On
RewriteRule ^/$ /wordpress/ [PT]
The rewrite was occurring in the last line of this file:
RewriteRule ^/$ /wordpress/ [PT]
I modified this file to the following and the redirects are working now:
# App url moved to root
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
#Alias /wordpress/ "/opt/bitnami/apps/wordpress/htdocs/"
#Alias /wordpress "/opt/bitnami/apps/wordpress/htdocs"
<Directory "/opt/bitnami/apps/wordpress/htdocs">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
#RewriteBase /wordpress/
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<//Directory>
# Uncomment the following lines to see your application in the root
# of your URL. This is not compatible with more than one application.
#RewriteEngine On
#RewriteRule ^/$ /wordpress/ [PT]

Resources