简体   繁体   English

.htaccess将www和非www映射到文件夹并维护www

[英].htaccess mapping www & non-www to folder and maintain www

I am no .htaccess expert and extremely rarely use it. 我不是.htaccess专家,并且很少使用它。 I went through several solutions on Net and SO but no luck still. 我在Net和SO上经历了几种解决方案,但是仍然没有运气。 So I have one IP says 1.2.3.4 that has sub folder of my wordpress application says ABC in the /var/www/html/ . 所以我有一个IP说1.2.3.4 ,我的wordpress应用程序的子文件夹在/var/www/html/说ABC。 DNS has been setup so that www.mydomain.com and mydomain.com point to IP 1.2.3.4 . DNS已设置为使www.mydomain.commydomain.com指向IP 1.2.3.4

I have my content in .htaccess file as below and it only supports mydomain.com . 我的内容在.htaccess文件中,如下所示,它仅支持mydomain.com The content is displayed as expected except when I put www.mydomain.com it displays Linux home page. 内容按预期显示,但当我输入www.mydomain.com它将显示Linux主页。

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteCond %{REQUEST_URI} !^/ABC/
RewriteRule (.*) /ABC/$1

When I put www.mydomain.com the browser automatically change the URL to mydomain.com . 当我放置www.mydomain.com ,浏览器会自动将URL更改为mydomain.com

Apart from that, I would also like to have all domain with www . 除此之外,我还希望所有域名都带有www Means request from mydomain.com will be displayed as www.mydomain.com . 意味着来自mydomain.com请求将显示为www.mydomain.com Later I will just put www.mydomain.com in the URL config of my wordpress. 稍后,我将把www.mydomain.com放在wordpress的URL配置中。 Some helps are appreciated. 一些帮助表示赞赏。

Switch back to your old .htaccess code. 切换回您的旧.htaccess代码。 I got it more clear after your last update. 上次更新后,我更加清楚了。 You have wordpress files in "public_html/ABC" but need your website url as www.example.com. 您在“ public_html / ABC”中有wordpress文件,但需要您的网站网址为www.example.com。 Let me explain step by step. 让我逐步解释。

  1. Paste your old code in .htaccess. 将您的旧代码粘贴到.htaccess中。
  2. copy (do not move) the index.php and .htaccess file from the /ABC folder to the "public_html" of your site. 将index.php和.htaccess文件从/ ABC文件夹复制(不要移动)到站点的“ public_html”。
  3. Find one rule in "public_html/index.php" as "require( dirname( FILE ) . '/wp-blog-header.php' );" 在“ public_html / index.php”中找到一条规则,作为“ require(dirname( FILE )。'/wp-blog-header.php');” and replace it with "require( dirname( FILE ) . '/ABC/wp-blog-header.php' );" 并将其替换为“ require(dirname( FILE )。'/ABC/wp-blog-header.php');”; (do not edit the index.php file in the folder /ABC!!) (请勿编辑/ ABC文件夹中的index.php文件!)
  4. change ONLY the "Site Address (URL)" in the general settings (Settings -> General) to " http://www.example.com ". 仅将常规设置(设置->常规)中的“站点地址(URL)”更改为“ http://www.example.com ”。

So finally I managed to get what I want to be working with below solution. 所以最后我设法得到了我想要与以下解决方案一起使用的东西。 Hopefully this will help somebody out there: 希望这可以帮助某人:

RewriteEngine on
RewriteBase /

# Whatever request coming to the server, add www to it
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Map the www URL to the subfolder in root directory    
RewriteCond %{HTTP_HOST} www\.myurl\.com$
RewriteCond %{REQUEST_URI} !^/thefolder/
RewriteRule (.*) /thefolder/$1

For Wordpress part, I have put codes in wp-config.php as below: 对于Wordpress,我将代码放在wp-config.php ,如下所示:

define('WP_HOME','http://www.myurl.com');
define('WP_SITEURL','http://www.myurl.com');

Thank you. 谢谢。 Cheers. 干杯。

UPDATE I have just realized that after the changes above, I could not able to login to my WP admin page. 更新我刚刚意识到,经过上面的更改,我无法登录到我的WP管理页面。 This is due to wrong redirect. 这是由于错误的重定向。 It looks something like http://www.myurl.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.myurl.com%2Fthefolder%2Fwp-admin%2F&reauth=1 看起来像http://www.myurl.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.myurl.com%2Fthefolder%2Fwp-admin%2F&reauth=1

When I remove the thefolder from URL it redirects to expected page. 当我从URL中删除文件thefolder ,它将重定向到预期的页面。 Can somebody advice on this? 有人可以建议吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM