Hide subdir in displayed URL via .htaccess?

2019-02-21 03:09发布

问题:

My client's domain name points to /public_html; his website is at /public_html/wp. I have a .htaccess that redirects to /public_html/wp:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?somedmain\.com$
RewriteCond %{REQUEST_URI} !^/wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wp/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?somedmain\.com$
RewriteRule ^$ /wp/ [L]   

However, when the user navigates to a page, the url is displayed as somedmain.com/wp/permalink.

I've been trying to figure how to hide the "wp" in the url; is that possible?
Thanks for any hints....

回答1:

There is no need for redirection. It can be achieved directly in Wordpress.

The way to have a WP install with the core files in one directory, but show another directory in the browser's address bar, is described below:

In General Settings there are 2 URLs, WordPress Address (URL) and Site Address (URL):

  1. The Site Address (URL) setting is the address you want people to type in their browser to reach your WordPress blog.
  2. The WordPress Address (URL) setting is the address where your WordPress core files reside.

The whole process is explained in this document Giving_WordPress_Its_Own_Directory.

For this example, the current WP install is at http://example.com/wp and the address to enter and show in the browser's address bar is http://example.com.

  1. Go to Settings -> General.
  2. In Site Address (URL), enter the directory to show: http://example.com in this case. NO trailing slash.
  3. Save changes and log out.
  4. Copy .htaccess and index.php files from http://example.com/wp to http://example.com.
  5. Open index.php at http://example.com and replace:

require('./wp-blog-header.php'); with

require('./wp/wp-blog-header.php');

6. Login using http://example.com/wordpress/wp-admin/.
7. Go to the permalinks settings to update the Permalink structure.

That's all. The WP blog is accessed at http://example.com from now on..