ModRewrite 在删除 WWW 时出现奇怪的重定向行为

发布于 2024-09-03 21:14:58 字数 1784 浏览 3 评论 0原文

我试图在我的项目中使用一些规则来从 URL 开头删除 www,但我遇到了一些问题。

我的服务器结构是:

domain.com/beta_folder
domain.com/beta_folder/page+type
domain.com/beta_folder/page+type/content+name
domain.com/beta_folder/page+type/content+name/edit
domain.com/beta_folder/page+type/content+name/etc.

domain.com/beta_folder/.htaccess //here is where my htaccess is

beta_folder 是站点文件夹,content+name 是内容变量,创建用于从数据库检索页面。

该网站与此规则完美配合

RewriteEngine On
RewriteRule ^(page\+type/)([a-zA-Z0-9_+-]+)[/]?$ page_folder/page.php?varname=$2

我的目的是删除 www,因此我添加了此规则,但它无效

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com$1 [R=301,L]
RewriteRule ^(page\+type/)([a-zA-Z0-9_+-]+)[/]?$ page_folder/page.php?varname=$2

如果我在前面输入 www,我的问题就会出现我的域名:

此方法

http://domain.com/beta_folder/page+type/content+name

如果我删除 www 规则,则如果我编写

http://www.domain.com/beta_folder/page+type/content+name

重写规则将我重定向,则

http://www.domain.compage+type/content+name

有效,不幸的是,问题仍然存在

,我基本上无法对我的域进行公开测试

,如果我编写的

 http://www.domain.com/beta_folder

规则将我发送到我

 http://domain.com/

发现解决问题的唯一方法是写入我的网站所在的文件夹,所以:

RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/beta_folder/$1 [R=301,L]

我不明白为什么我应该写入该文件夹,因为我的重写条件会影响 http://www.domain.com/folder_name/contents 而不是 http://www.domain.com/contents

为什么缺少 folder_name从重写?

I'm trying to use some rule on my project to remove www from the beginning of the URL but I've some problem.

my server structure is:

domain.com/beta_folder
domain.com/beta_folder/page+type
domain.com/beta_folder/page+type/content+name
domain.com/beta_folder/page+type/content+name/edit
domain.com/beta_folder/page+type/content+name/etc.

domain.com/beta_folder/.htaccess //here is where my htaccess is

beta_folder is the site folder, and content+name are content vars, created to retrieve pages from the database.

the site works perfect with this rules

RewriteEngine On
RewriteRule ^(page\+type/)([a-zA-Z0-9_+-]+)[/]?$ page_folder/page.php?varname=$2

My intention was to remove www, so I've added this rule but it isn't effective

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com$1 [R=301,L]
RewriteRule ^(page\+type/)([a-zA-Z0-9_+-]+)[/]?$ page_folder/page.php?varname=$2

My problem starts if I digit www in front of my domain name:

this works

http://domain.com/beta_folder/page+type/content+name

if i write

http://www.domain.com/beta_folder/page+type/content+name

the rewrite rule redirect me at

http://www.domain.compage+type/content+name

if i remove the www rules, the problem still active

unfortunately, I can't make a public test for my domain

basically, if I write

 http://www.domain.com/beta_folder

the rules sends me to

 http://domain.com/

the only way I've found to solve the problem is to write the folder where my site is, so:

RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/beta_folder/$1 [R=301,L]

I don't undestand why I should write the folder, because my rewrite cond affects
http://www.domain.com/folder_name/contents and not http://www.domain.com/contents

why folder_name missing from the rewriting?

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

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

发布评论

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

评论(1

柒夜笙歌凉 2024-09-10 21:14:58

试试这个与您的规则略有不同的示例:

RewriteCond %{HTTP_HOST} !^www.domain\.com$
RewriteRule (.*) http://domain.com/$1 [R=301,L]

如果这不能解决您的问题,请大声喊出来。

Try this, subtly different example of your rule:

RewriteCond %{HTTP_HOST} !^www.domain\.com$
RewriteRule (.*) http://domain.com/$1 [R=301,L]

Shout out if this doesn't solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文