使用 Mod_Rewrite 子域转发到 URL?

发布于 2024-12-25 12:42:57 字数 553 浏览 2 评论 0原文

我目前正在使用 mod_rewrite,但希望使用特定的 URL 路径来使用子域名。这可以通过 mod_rewrite 实现吗?

目前 URL 是这样的:

http://domain.com/index.php?wiki/index/ -> http://domain.com/forum/wiki

How can I get the url to be

http://wiki.domain.com/forum/ 

当 wiki 目录不存在时, ?我不断收到 403 错误,因为该目录不存在,因此它是被禁止的。我目前有:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^wiki.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^wiki.domain.com$
RewriteRule ^(.*)$ http://domain.com/wiki/index[R=301,L]

I'm currently using mod_rewrite but would like a specific URL path to use a subdomain name. Would this be possible with mod_rewrite?

Currently the URL is like this:

http://domain.com/index.php?wiki/index/ -> http://domain.com/forum/wiki

How can I get the url to be

http://wiki.domain.com/forum/ 

when the wiki directory does not exist? I keep getting a 403 as the directory does not exist and so it's forbidden. I currently have:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^wiki.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^wiki.domain.com$
RewriteRule ^(.*)$ http://domain.com/wiki/index[R=301,L]

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

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

发布评论

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

评论(2

心碎的声音 2025-01-01 12:42:57

这是一个基于 mod_rewrite 的解决方案,只需将这些行放入您的 .htaccess 文件中:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^forum/wiki/?$ http://wiki.domain.com/forum/ [R=301,L,NC]

这会将 http://domain.com/forum/wiki 重定向到 http://wiki.domain .com/forum/

Here is a solution based on mod_rewrite, just put these lines in your .htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^forum/wiki/?$ http://wiki.domain.com/forum/ [R=301,L,NC]

This will redirect http://domain.com/forum/wiki to http://wiki.domain.com/forum/

青衫负雪 2025-01-01 12:42:57

提示如下:您实际上不需要进行重定向

只需拥有一个处理两个域的虚拟主机即可。

只需进行内部重写即可。

这是我自己的虚拟主机(处理domain.com和所有*.domain.com)和你想要的(这个可能无法 100% 工作,但是嘿,我在此之后给您两个提示):

<VirtualHost *>

    ServerAdmin [email protected]
    DocumentRoot "/web/htdocs/domain/prod/website"

    ServerName domain.com
    ServerAlias *.domain.com
    ErrorLog "/web/logs/domain.error.log"
    CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/domain/access_log.%Y-%m-%d-%H_%M_%S.log 5M" combined

    # your RewriteRules ()
    RewriteEngine On

    # Trace:
    # (!) file gets big quickly, remove in prod environments:
    RewriteLog "/web/logs/domain.rewrite.log"
    RewriteLogLevel 9

    # Your RewriteRules ()
    RewriteCond %{HTTP_HOST} ^wiki.domain.com$
    RewriteRule ^/forum/(.*) /wiki/index.php?wiki/index$1 [QSA,L]

</VirtualHost>

两个提示:

如果您不在托管环境中(= 如果它是您自己的服务器,并且您可以修改虚拟主机,而不仅仅是 . .htaccess文件),尝试使用 RewriteLog 指令:它可以帮助您追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我最喜欢的检查正则表达式的工具:

http://www.quanetic.com/Regex (不要忘记选择 ereg(POSIX) 而不是 preg(PCRE)!)

Here's the tip: you don't need to do a redirect actually.

Just have one vhost that handles both domains.

Just do an internal rewrite, and it will work.

Here's a mix of my own vhost (that handles domain.com and all *.domain.com) and what you'd like (this may not work 100% but hey I give you two hints after this):

<VirtualHost *>

    ServerAdmin [email protected]
    DocumentRoot "/web/htdocs/domain/prod/website"

    ServerName domain.com
    ServerAlias *.domain.com
    ErrorLog "/web/logs/domain.error.log"
    CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/domain/access_log.%Y-%m-%d-%H_%M_%S.log 5M" combined

    # your RewriteRules ()
    RewriteEngine On

    # Trace:
    # (!) file gets big quickly, remove in prod environments:
    RewriteLog "/web/logs/domain.rewrite.log"
    RewriteLogLevel 9

    # Your RewriteRules ()
    RewriteCond %{HTTP_HOST} ^wiki.domain.com$
    RewriteRule ^/forum/(.*) /wiki/index.php?wiki/index$1 [QSA,L]

</VirtualHost>

Two hints:

If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

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