.htaccess - 强制 https、强制到子文件夹、强制 www,不要破坏站点

发布于 2024-10-13 03:43:29 字数 3377 浏览 3 评论 0原文

我一直很难编辑 .htaccess 文件来同时完成这三件事。我已经能够分别获得每个部分,但我只是不明白逻辑流程如何让它们全部工作。

这是我使用 bluehost 支持上的演示所能收集到的最好的结果 http://helpdesk.bluehost.com/index.php/kb/article/000347

感谢您为解决此问题提供的任何帮助。

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
# Bluehost.com
# .htaccess main domain to subdirectory redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
# Do not change this line.
RewriteEngine on
# Change yourdomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/folder/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /folder/$1
# Change yourdomain.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your folder, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$
RewriteRule ^(/)?$ folder/index.php

RewriteCond %{HTTP_HOST} ^sampleurl\.com$ [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.sampleurl.com/$1 [L]

# For security reasons, Option all cannot be overridden.
# Options All -Indexes
# Options ExecCGI Includes IncludesNOEXEC SymLinksIfOwnerMatch -Indexes

更新:

经过很长一段时间使用以下内容后,我遇到了问题。

IndexIgnore *
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule (.*) https://www.example.com/site/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/site/
RewriteRule (.*) https://www.example.com/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule (.*) https://www.example.com/site/$1 [L,R]

新问题。

  1. Bluehost 有一个应用程序,可以快速安装和更新开源应用程序,如 wordpress、joomla、phpbb 等。显然我的新超级 .htaccess 文件完全破坏了他们的系统。我可以运行更新和安装的唯一方法是将 .htaccess 移动到 .htaccess.bak,直到升级或安装完成。然后将其移回去。

  2. 我无法在其他目录中托管的其他网站上创建任何 .htaccess。例如,我有一个主帐户,存储在 /www/site/ 中,该脚本强制将请求发送到该帐户中。不幸的是,我存储在 /www/site2/ 和 /www/site3/ 中的其他网站无法正常工作。首先,我必须创建一个空白的 .htaccess 以防止它拉出上面在 /www/ 中找到的 .htaccess 。出于某种原因,如果我尝试执行诸如强制 www.site2.com 之类的操作,我会收到 500 错误,指出我强制进行了太多重定向。

我有一个理论,我上面的解决方案需要更具体一点,以确保它只影响发送到 example.com 的请求。 (我认为不包含 /site/ 的任何内容的界限有点​​宽泛)。

我感谢任何解决此问题的支持。会让我省去一些头痛!

更新二:

我不相信我有权访问 apache 配置。我在 cpanel 中看到的唯一选项是 apache 处理程序(不太确定该部分的作用)。

我将提交一张票以查看 simplescripts 服务器从哪个位置运行。我相信它从“https://www.simplescripts.com”运行。您建议我添加什么编辑来忽略此域?

我的主机 bluehost 首先使用主帐户,然后使用子域。主帐户的位置设置为 user\www。不幸的是,当您尝试托管多个域时,这会变得混乱。这就是为什么我使用 .htaccess 强制将访问该目录的请求放入 \site\ 目录。从而将主站点捕获到 \site\ 目录中。

我认为我的一个子域遇到的搬迁问题是由于 wordpress 试图强制一个没有 www 的域,然后我编写了一个 .htaccess 文件强制 www。这导致了无限循环,而不是该线程所涉及的我的 .htaccess 文件。如果我能解决 simplescripts 问题,我就是 gmoney ;)

I have been having a very difficult time editing my .htaccess file to do these three things together. I have been able to get each part separately but I just do not understand how the logic flow works to get them all to work.

This is the best I have been able to pull together using the demo at bluehost support
http://helpdesk.bluehost.com/index.php/kb/article/000347

I appreciate any help resolving this issue.

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
# Bluehost.com
# .htaccess main domain to subdirectory redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
# Do not change this line.
RewriteEngine on
# Change yourdomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/folder/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /folder/$1
# Change yourdomain.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your folder, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?sampleurl.com$
RewriteRule ^(/)?$ folder/index.php

RewriteCond %{HTTP_HOST} ^sampleurl\.com$ [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.sampleurl.com/$1 [L]

# For security reasons, Option all cannot be overridden.
# Options All -Indexes
# Options ExecCGI Includes IncludesNOEXEC SymLinksIfOwnerMatch -Indexes

Update:

Well after a long time using the following I have run into issues.

IndexIgnore *
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule (.*) https://www.example.com/site/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/site/
RewriteRule (.*) https://www.example.com/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule (.*) https://www.example.com/site/$1 [L,R]

New Issues.

  1. Bluehost has an application that quickly installs and updates open source applications like wordpress, joomla, phpbb etc. Well apparently my new super .htaccess file completely breaks their system. The only way I can run updates and installs is to move the .htaccess to .htaccess.bak until the upgrade or install is complete. Then move it back.

  2. I can't create any .htaccess on other sites that I host in other directories. e.g. I have a master account that I store in /www/site/ that this script forces requests into. Unfortunately my other sites stored in /www/site2/ and /www/site3/ don't work well. First I have to create a blank .htaccess to prevent it from pulling the .htaccess above found in /www/. For some reason if I try to do anything like force www.site2.com I get 500 error's saying I am forcing too many redirects.

I have a theory that my solution above needs to be a little more specific, to make sure it only effects request sent to example.com. (I think the line with anything not containing /site/ is a little to broad).

I appreciate any support resolving this. Would save me a bit of headaches!

Update II:

I do not believe I have access to apache config. Only option I see in cpanel is apache handlers (not really sure what that section does).

I will submit a ticket to see what location the simplescripts server runs from. I believe it runs from "https://www.simplescripts.com". What edit would you recommend I add to ignore this domain?

My host bluehost starts you with a primary account and then subdomains. The primary account's location is set at user\www. Unfortunatly this gets messy when you try to host multiple domains. That is why I use .htaccess to force requests that hit that directory into the \site\ directory. Thus trapping the primary site into the \site\ directory.

I think the relocation issue I was having with one of my subdomains was due to wordpress trying to force a domain without www and then me writing an .htaccess file forcing www. That caused the infinite loop, not my .htaccess file this thread is about. If I can resolve the simplescripts issue I am gmoney ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怕倦 2024-10-20 03:43:29

我认为这样的东西会起作用

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/folder/
RewriteRule (.*) https://www.example.com/$1 [L,R]

,但我承认我还没有测试过。如果需要,您可以添加所有“如果文件存在则不重定向”等。

如果您还没有看过,这里是mod_rewrite 文档

编辑:

响应更新的问题:

我注意到的第一件事是,您似乎对您的设置有足够的控制权,可以将重写放在 Apache 配置文件(在 VirtualHost 部分)中,而不是放在htaccess 文件,这将立即解决您与其他网站的问题。

第一个问题,听起来您需要添加 RewriteCond 以排除 Bluehost 软件使用的任何 URL。或者,或者在不同的 VirtualHost 下访问 Bluehost 软件(一旦您将重写内容放入 VirtualHost 块中)。

如果您无法编辑您的 Apache 配置,那么对其他站点最简单的修复就是不要将它们设为主站点的子目录(在文件系统上)。无论如何,这很奇怪。如果需要,也将您的主站点移动到 /www 的子目录中。

就我个人而言,我将所有站点放在我的服务器上的 /srv/www/com.site-name.www 中(即相反的顺序,顶级域在前,它使相关的内容组按字母顺序排序时放在一起,例如,当您有 www.example.com 和 static.example.com 时)

I'd think something like this would work

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/folder/
RewriteRule (.*) https://www.example.com/$1 [L,R]

but I confess I haven't tested. You can add in all the don't redirect if the file exists, etc., if you want.

In case you haven't seen it, here is the mod_rewrite documentation.

edit:

In response to updated question:

The first thing I notice is that it appears you may have enough control over your setup to put the rewrites in the Apache config files (in the VirtualHost sections) instead of in htaccess files, that'll immediately solve your problem with your other sites.

The first problem, it sounds like you'll need to add RewriteCond to exclude whichever URLs the Bluehost software uses. Either that, or access the Bluehost software under a different VirtualHost (once you've put the rewrite stuff inside the VirtualHost blocks).

If you can't edit your Apache config, then the easiest fix for the other sites it to not make them a subdirectory (on the filesystem) of your main site. That's rather weird, anyway. Move your main site to a sub-directory of /www as well if need be.

Personally, I put all sites in /srv/www/com.site-name.www on my servers (that is, reverse order, with the top-level-domain first, it makes related stuff group together when sorted alphabetically. E.g., when you have www.example.com and static.example.com)

∞觅青森が 2024-10-20 03:43:29

这似乎可以满足我需要的一切。可能是完成此任务的更短方法,但它有效!
索引忽略 *
重写引擎开启

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/folder/
RewriteRule (.*) https://www.example.com/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]

This seems to do everything I needed. Probably a shorter way to get this done but it works!
IndexIgnore *
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/folder/
RewriteRule (.*) https://www.example.com/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule (.*) https://www.example.com/folder/$1 [L,R]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文