通配符子域问题与其他页面的链接
使用通配符子域时,我在链接到页面时遇到问题,希望得到一些帮助来找出解决方案。
我希望用户拥有自己的子域来查看自己的网站内容,因此如果用户输入“user1.domain.com”,服务器将有一个重写规则,将请求定向到根目录中的index.php页面。该index.php页面将用于所有子域,并将根据查询字符串中传递的子域从数据库中提供内容。
这部分工作得很好,它按预期提供了 index.php 中的内容。当index.php页面加载时,它包含一个菜单(主页|租赁|联系我们)。当我单击菜单链接转到另一个页面时,问题就出现了。 URL 地址发生变化,但页面内容不变。
下面是一个示例:如果我单击“RENTALS”,我希望用户转到 Rentals.php 页面并显示“user1”子域可用的租金...但发生的情况如下:
链接场景 1:
<a href="http://user1.domain.com/rentals.php">RENTALS</a> --> URL address bar changes to "user1.domain.com/rentals.php" but the page doesn't change, it still stays on the index.php content
链接场景 2 :
< a href="rentals.php" >RENTALS< /a > --> URL address bar changes to "user1.domain.com/rentals.php" but the page doesn't change, it still stays on the index.php content
链接场景 3(如果我对域进行硬编码并省略通配符子域,则链接有效):
<a href="http://www.domain/public_site/rentals.php">RENTALS</a> --> URL address bar changes to "http://www.domain.com/public_site/rentals.php" AND IT WORKS, the rentals.php page loads and serves up the content.
场景 3 的问题是我希望 url 为:“user1.domain.com/rentals.php”
我想知道是否可以在重写条件下设置它,但我无法弄清楚。
如果有人可以帮助我,我将不胜感激!
这是我的目录结构:
ROOT
|
PUBLIC_SITE
|
index.php
rentals.php
contactus.php
这是 apache 重写规则(第一条规则有效。我添加的第二条规则希望解决我的问题,但它没有解决问题):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
####Rewrite subdomain request to public_site/index.php
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^(.*)$ /public_site/index.php?subdomain=%2 [L]
#### Rewrite rule for rentals.php page
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com/rentals.php [NC]
RewriteRule ^(.*)$ /public_site/rentals.php?s=%2 [L]
</IfModule>
I’m having troubles linking to pages when using a wildcard subdomain and would love a little help figuring out a solution.
I want the users to have their own subdomain to view their own website content so if a user types in "user1.domain.com" the server would have a rewrite rule that directs the request to the index.php page in the root directory. This index.php page will be used for all subdomains and will serve the content out of a db based on the subdomain passed in the querystring.
This part is working great and it serves up the content from index.php as it should. When index.php page loads it includes a menu (HOME | RENTALS | CONTACT US). The problem comes when I click on the menu link to go to another page. The URL address changes, but the page content does not.
Here's an example: If I click on "RENTALS" I want the user to go to the rentals.php page and display the rentals available for the "user1" subdomain...but here's what is happening:
Link scenario 1:
<a href="http://user1.domain.com/rentals.php">RENTALS</a> --> URL address bar changes to "user1.domain.com/rentals.php" but the page doesn't change, it still stays on the index.php content
Link scenario 2:
< a href="rentals.php" >RENTALS< /a > --> URL address bar changes to "user1.domain.com/rentals.php" but the page doesn't change, it still stays on the index.php content
Link scenario 3 (if I hardcode the domain and ommit the wildcard subdomain the link works):
<a href="http://www.domain/public_site/rentals.php">RENTALS</a> --> URL address bar changes to "http://www.domain.com/public_site/rentals.php" AND IT WORKS, the rentals.php page loads and serves up the content.
The problem with scenario 3 is I want the url to read: "user1.domain.com/rentals.php"
I'm wondering if this can be setup in a rewrite condition, but I'm not able to figure it out.
If anyone can help I would be grateful!
Here is my directory structure:
ROOT
|
PUBLIC_SITE
|
index.php
rentals.php
contactus.php
Here is the apache rewrite rule (the 1st rule works. the 2nd rule I added hoping to solve my problem, but it didn't solve the problem):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
####Rewrite subdomain request to public_site/index.php
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^(.*)$ /public_site/index.php?subdomain=%2 [L]
#### Rewrite rule for rentals.php page
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com/rentals.php [NC]
RewriteRule ^(.*)$ /public_site/rentals.php?s=%2 [L]
</IfModule>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
进入重写后我自己想出了办法......
只需将此代码放在“主域”规则之前:
figured it out on my own after getting into the rewrite...
simply put this code before the "main domain" rules: