htaccess 子域名(第 2 部分)

发布于 2024-11-01 01:18:20 字数 1801 浏览 0 评论 0原文

好吧,在我上一篇文章之后,我已经取得了一些进展,但我仍然需要帮助。 对于那些首先阅读这篇文章的人,这里有一个概要。

好的,项目希望每个注册成员都有一个唯一的 url(子域),例如用户 foobar 将拥有 foobar.example.com。

经过一番阅读后,我要求我的托管公司启用名称服务器和 Web 服务器来接受通配符请求。

我想我已经成功解决/创建了项目的这一部分。 现在我需要找到一种在子域中创建子页面的方法。

提供服务

目的是 demo.example.com 和 demo.example.com/ 和 demo.example.com/index.php 由 www.example.com/sub_index.php?user=demo demo.example.com/gallery.php 由 www.example.com/sub_gallery.php?user=demo

demo.example.com/gallery.php?page=2 提供服务www.example.com/sub_gallery.php?user=demo&page=2

子域中的所有页面都具有相同的模式:[sub_]。

基于以上,我构建了一些htaccess规则。但我没有得到好的结果。

实际上: 向 demo.example.com 发出的请求完美运行 向 demo.example.com/ 或 demo.example.com/index.php 发出的请求显示 example.com/index.php 对 demo.example.com/gallery.php 的请求会抛出 404 错误(尽管页面 sub_gallery.php 位于根文件夹中)。

我当前的 htaccess (位于根文件夹)是:

Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /

#rewrite example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]



###########################
##  subdomains
###########################
    #index
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^$ /sub_index.php?user=%1 [L]

    #other pages
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^/other_page.php$ /sub_other_page.php?user=%1 [QSA,L]

任何人都可以看到任何错误并提供一些反馈吗? 谢谢

Ok after my previous post, i have made some progress, yet still i need help.
For those that read this post first, here is a synopsis.

Ok, project wants each registered member to have a unique url (subdomain), for example user foobar will have foobar.example.com.

After some reading i have asked my hosting company to enabled name server and web server to accept wildcard requests.

This part of the project i think i have managed to solved/create.
Now i need to find a way to create subpages in subdomains.

Purpose is demo.example.com and demo.example.com/ and demo.example.com/index.php to be served by www.example.com/sub_index.php?user=demo

demo.example.com/gallery.php to be served by www.example.com/sub_gallery.php?user=demo

demo.example.com/gallery.php?page=2 to be served by www.example.com/sub_gallery.php?user=demo&page=2

All pages in subdomains have the same pattern: [sub_].

Based on the above, i have constructed some htaccess rules. But I get no good results.

Actually:
requests made to demo.example.com work perfect
requests made to demo.example.com/ OR demo.example.com/index.php show me the example.com/index.php
requests to demo.example.com/gallery.php throws 404 error (although page sub_gallery.php is in root folder).

My current htaccess (at root folder) is:

Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /

#rewrite example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]



###########################
##  subdomains
###########################
    #index
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^$ /sub_index.php?user=%1 [L]

    #other pages
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^/other_page.php$ /sub_other_page.php?user=%1 [QSA,L]

Can anyone see any error and provide some feedback?
Thank you

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

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

发布评论

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

评论(2

爱的那么颓废 2024-11-08 01:18:20

尝试将您的最后一条规则更新为以下内容。对于所请求的页面来说,它是动态的,gallery.php -> sub_gallery.php,我删除了前导 /,因为从 .htaccess 文件重写时它不存在。

RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*).php$ /sub_$1.php?user=%1 [QSA,L,R]

希望这有帮助。

Try updating your last rule to the following. It is dynamic for the requested page, gallery.php -> sub_gallery.php, and I removed the leading /, as that is not present when rewriting from the .htaccess file.

RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*).php$ /sub_$1.php?user=%1 [QSA,L,R]

Hopefully this helps.

り繁华旳梦境 2024-11-08 01:18:20

我只是想在这里发布最终的代码,以供将来的读者

#######################################################
##### rewrite example.com into www.example.com #####
######################################################
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

###########################
## subdomains
###########################
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*)\.php$ /sub_$1.php?user=%1 [QSA,L]

此代码将采用名为: sub_[PAGE].php?user=demo&id=5 的任何页面
并创建 demo.example.com/[PAGE].php?id=5

I just thought to post here the final code, for future readers

#######################################################
##### rewrite example.com into www.example.com #####
######################################################
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

###########################
## subdomains
###########################
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*)\.php$ /sub_$1.php?user=%1 [QSA,L]

This code will take any page named: sub_[PAGE].php?user=demo&id=5
and create demo.example.com/[PAGE].php?id=5

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