htaccess - 重定向以忽略嵌套子目录,如果可能,使整个站点非 www

发布于 2024-11-02 16:42:19 字数 3299 浏览 1 评论 0原文

我有一个对我来说太复杂而无法处理的问题,但我敢打赌以前有人必须这样做,所以请让我收到您的来信。 ;)

情况如下:

我有 1 个主域,其中有 3 个相互嵌套的子目录

(从上到下) http://main-domain.com 然后 http://main-domain.com/company-name/ 然后 http://main-domain.com/company-name/blog/

目前有 3 个 .htaccess 文件 - 上面显示的 3 个目录中各有 1 个。

有什么问题吗?

我不想拥有 www.main-domain.com/company-name/blog/whatever,而是想要 main-domain.com/blog/whatever

所以,我想要删除www,更重要的是,删除中间的子目录;即/company-name/

我希望下面的例子将有助于说明这一点。

http://main-domain.com/company-name/index.php 应更改为 http://main-domain.com/index.php

http://main-domain.com/company-name/blog/my-first-article/ 应更改为 http://main-domain.com/blog/my-first- Article/

为什么我需要这个?

我需要一个更短、更利于 SEO 的 URL。我有太多使用“旧”网址的反向链接,因此我需要对它们进行全部修改。

这是我当前的 3 个 htaccess 文件

根 htaccess:main-domain.com

#Bypass InoCore Templating System

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /reservations/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /reservations/default.php [L]

Options -Indexes

</IfModule>

#END Bypass

#301 REDIRECT

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^info.php - [L]

RewriteCond %{HTTP_HOST} ^www.domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www.domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

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

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

公司名称 htaccess:main-domain.com/company-name/

RewriteEngine on

RewriteRule ^maping.php /maping.php

RewriteRule ^$ index.php?$1 [L]

RewriteRule (.*) index.php?$1 [L]

#php_flag magic_quotes_gpc off

#BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /company-name/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/index.php [L]

</IfModule>

#END WordPress

博客htaccess:main-domain.com/company-name/blog/

RewriteEngine off

#BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /company-name/blog/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/blog/index.php [L]

</IfModule>

#END WordPress

I have an issue that's too complex for me to handle, but I'm betting someone has had to do this before, so please let me hear from you. ;)

Here's the situation:

I've got 1 main domain with 3 subdirectories that are nested within each other

(from top to bottom)
http://main-domain.com
then
http://main-domain.com/company-name/
then
http://main-domain.com/company-name/blog/

There's currently 3 .htaccess files -- 1 in each of the 3 directories shown above.

What's the problem?

Instead of having www.main-domain.com/company-name/blog/whatever, I'd like to have main-domain.com/blog/whatever

So, I want to drop the www AND more importantly, drop the middle subdirectory; i.e. /company-name/

I hope that the following examples will help to illustrate the point.

http://main-domain.com/company-name/index.php should be changed to http://main-domain.com/index.php

http://main-domain.com/company-name/blog/my-first-article/ should be changed to http://main-domain.com/blog/my-first-article/

Why do I need this?

I need a shorter URL that is more SEO-friendly. I have too many backlinks that use the 'old' urls, so I need to mod-rewrite them all.

Here are My Current 3 htaccess files

root htaccess: main-domain.com

#Bypass InoCore Templating System

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /reservations/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /reservations/default.php [L]

Options -Indexes

</IfModule>

#END Bypass

#301 REDIRECT

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^info.php - [L]

RewriteCond %{HTTP_HOST} ^www.domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www.domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

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

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

company-name htaccess: main-domain.com/company-name/

RewriteEngine on

RewriteRule ^maping.php /maping.php

RewriteRule ^$ index.php?$1 [L]

RewriteRule (.*) index.php?$1 [L]

#php_flag magic_quotes_gpc off

#BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /company-name/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/index.php [L]

</IfModule>

#END WordPress

blog htaccess: main-domain.com/company-name/blog/

RewriteEngine off

#BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /company-name/blog/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/blog/index.php [L]

</IfModule>

#END WordPress

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

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

发布评论

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

评论(1

余厌 2024-11-09 16:42:19

您正确且紧凑的根 .htaccess 应该是这样的:

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^info.php - [L]

# match all the domains in single condition while www. is optional
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3|main-domain)\.(com|tld)$ [NC]
RewriteRule ^company-name/(.*)$ http://www.domain.tld/$1 [R=301,L,NC,NE]
  • R=301 将使用 https 状态 301 进行重定向
  • L 将制定最后一条规则
  • NE 用于无转义查询字符串
  • NC 用于忽略大小写比较
  • $1 是您的 REQUEST_URI 匹配组

Your correct and compact root .htaccess should be like this:

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^info.php - [L]

# match all the domains in single condition while www. is optional
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3|main-domain)\.(com|tld)$ [NC]
RewriteRule ^company-name/(.*)$ http://www.domain.tld/$1 [R=301,L,NC,NE]
  • R=301 will redirect with https status 301
  • L will make last rule
  • NE is for no escaping query string
  • NC is for ignore case comparison
  • $1 is your REQUEST_URI matching group
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文