I have mentioned before that there are many methods you can choose during the WordPress installation phase Subdirectory. Of course, it can be installed quickly through Softaculous and similar applications.
When you download from WordPress1 or GitHub2 with wget or curl, you can see that the archive content will be kept in a folder content (wordpress or WordPress-x) unless otherwise specified. For example, let’s say we download the latest.zip archive. When we unzip the downloaded archive, a folder called wordpress will be created. To edit this situation in a simple way, we can use the command:
unzip -qq latest.zip && mkdir test && mv ./wordpress/* ./test/ && rm -rf wordpress latest.zip
The above command transfers the content of the wordpress folder created after the latest.zip archive to another folder named test and then deletes both the wordpress folder and the latest.zip archive. If we want the files to be opened directly to the current directory instead of the test in the command, we can update our command as follows:
unzip -qq latest.zip && mv ./wordpress/* ./ && rm -rf wordpress latest.zip
For example, let’s download the content of our examplewebsite.com web address and protect the wordpress folder. In this case, our wordpress installation processes will be published on examplewebsite.com/wordpress unless we specify otherwise. Of course, if we define a subdomain, we can publish as wordpress.examplewebsite.com. In this case, the simplest solution is to move the wordpress content to a parent directory (webroot directly; html, public_html, www, etc.), ie keeping the files in /var/www/html/worpdress under /var/www/html/. However, this is not the only method.
Editing .htaccess
Yes, we can view the WordPress installation in a different folder directly from the main URL by updating the .htaccess file. For this process, we must first move the .htaccess file to the webroot directory. Then we have to edit the file contents as follows.
RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?ornekwebsitesi.com$ RewriteCond %{REQUEST_URI} !^/worpdress/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.\*)$ /wordpress/$1 RewriteCond %{HTTP_HOST} ^(www.)?ornekwebsitesi.com$ RewriteRule ^(/)?$ wordpress/index.php \[L\]
You can also use this process for web domain name changes. If for some reason you have problems with wp-admin access, you can make the relevant changes by adding the WP_HOME and WP_SITEURL lines to wp-config.php.
define( ‘WP_HOME’, ‘http://examplewebsite.com/wordpress’ );
define( ‘WP_SITEURL’, ‘http://examplewebsite.com’ );
With update_option, it is possible to perform the above operation with the functions.php file included in the active theme.
update_option( ‘siteurl’, ‘http://examplewebsite.com/wordpress’ );
update_option( ‘home’, ‘http://examplewebsite.com’ );
Another alternative is to use the wp option command via WP-CLI.
wp option update home ‘http://examplewebsite.com/wordpress’
wp option update siteurl ‘http://examplewebsite.com’
When you want to make changes to the site address, you can make all changes at once with wp search-replace.
wp search-replace ‘examplewebsite.com’ ‘newexamplewebsite.com’ –skip-columns=guid
Apart from the above operations, there are many alternatives (like SQL commands). For other related alternatives, you can take a look at Changing The Site URL3 and Giving WordPress Its Own Directory4. All these processes are valid for cases where we provide wordpress access via subdirectory and / or directly from the main domain. So, what should we do if we want to use a subdomain? Of course, the same operations for folder arrangements (keeping the wordpress installation in a different folder, etc.) will remain valid. The additional arrangements we need to make are to create a subdomain via cPanel and similar management systems (use of wilcard, of course, I recommend) and edit the .htaccess content as necessary5.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ – [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
So what do we do when we have your installation in a folder named wordpress but we want to be accessed via altsite.examplewebsite.com?
It will be enough to edit the .htaccess content directly in the wordpress folder as follows.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The main thing to do is to specify the directory containing the WordPress files as the folder path after determining the subdomain during the creation of the subdomain via cPanel and similar management systems. In other words, we would be viewing our site in the content of /var/www/html/wordpress via the site.ornekwebsite.com address. Yes, it is possible to manage our WordPress subdirectory and subdomain configurations in different ways within different scenarios.