在 .htaccess 中为文件夹创建 301 重定向到子域并将 www 重定向到非 www

发布于 2025-01-02 02:43:48 字数 564 浏览 1 评论 0原文

我到处搜索并使用了一些示例,但它们不能满足我的特定需求,因此为什么我在这里询问是否有人可以提供帮助?我知道下面的 http:: 不正确,只是因为我无法在这里发布链接。

我想将 http:://www.mysite.co.uk/ 重定向到 http:://mysite.co.uk/ 同时也能够重定向

  1. http://www.mysite.co.uk/mysub/ 2. http://mysite.co.uk/mysub/ 到 3. http:://mysub.mysite.co.uk/

1. 和 2. 中的所有文件应重定向到 3. 中的等效文件。

例如:http:://www.mysite.co.uk/mysub /file.html 和 http:://mysite.co.uk/mysub/file.html 都应该转到 http:://mysub.mysite.co.uk/file.html

这仅影响子域,所以站点根目录中的其他文件夹和文件不受影响。

如果有人可以帮助我理解并在 .htaccess 文件中编写 301 重定向的代码,我将非常感激!谢谢!

I've searched everywhere and used some examples but they don't meet my specific needs, hence why I'm asking here if anyone can please help? I know the http:: below isn't correct, it's just because I can't post links here.

I'd like to redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
whilst also being able to redirect

  1. http:://www.mysite.co.uk/mysub/
    and 2. http:://mysite.co.uk/mysub/
    to 3. http:://mysub.mysite.co.uk/

All files in 1. and 2. should be redirected to their equivelant in 3.

For example: http:://www.mysite.co.uk/mysub/file.html AND http:://mysite.co.uk/mysub/file.html should both go to http:://mysub.mysite.co.uk/file.html

This is to affect the subdomain only, so that other folders and files in the site root aren't affected.

If anyone could help me understand and write the code for the 301 redirect in a .htaccess file I'd really really appreciate it! Thanks!

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

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

发布评论

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

评论(1

橙味迷妹 2025-01-09 02:43:48

这是您在 .htaccess 文件中需要的代码:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

# To redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
RewriteCond %{HTTP_HOST} ^www\.(mysite\.co\.uk)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# To redirect /mysub/foo to http://mysub.mysite.co.uk/foo
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.co\.uk)$ [NC]
RewriteRule ^(mysub)/?(.*)$ http://$1.%2/$2 [R=301,L,NC]

This is the code you'll need in your .htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

# To redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
RewriteCond %{HTTP_HOST} ^www\.(mysite\.co\.uk)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# To redirect /mysub/foo to http://mysub.mysite.co.uk/foo
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.co\.uk)$ [NC]
RewriteRule ^(mysub)/?(.*)$ http://$1.%2/$2 [R=301,L,NC]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文