php - 使用 5 个页面的 mod-Rewrite(通配符子域)

标签 php .htaccess mod-rewrite subdomain wildcard

假设我有 5 页(index.phpabout.phpportfolio.phpgallery.phpcontact.php)。

假设 ID 是 1 .

原来的网址是:

www.domain.com/folder/index.php?id=1 , www.domain.com/folder/about.php?id=1 等等……

现在,我想将其更改为:

1.domain.com/index , 1.domain.com/about , 1.domain.com/portfolio , 1.domain.com/gallery , 1.domain.com/contact

mod-rewrite 可以吗?如果我在 index.php页面,什么是<a href="" >转到about.php mod-rewrite 是否可行?

非常感谢你 :D 感谢你的帮助 :D

编辑

还有一件事,我的通配符子域文件夹是 public_html/folder/

解决方案 Anubhava

使用子域代码编辑:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# EDIT BY ANUBHAVA: to make http://1.domain.com/ load /about as default page
# Replace /about with any other page you want as default page
RewriteCond %{HTTP_HOST} ^1\.(domain\.com$ [NC]
RewriteRule ^$ /about [L]

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^(index|about|portfolio|gallery|contact)/?$ /$1.php?id=%2 [L,NC,QSA]

确保您的通配符子域目录是页面所在的位置。还要将 .htaccess 文件放在页面所在的目录中。

最佳答案

通过 httpd.conf 启用 mod_rewrite 和 .htaccess然后将此代码放入您的 .htaccessDOCUMENT_ROOT 下目录:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# EDIT: to make http://1.domain.com/ load /about
RewriteCond %{HTTP_HOST} ^1\.(domain\.com$ [NC]
RewriteRule ^$ /about [L]

RewriteCond %{HTTP_HOST} ^1\.(domain\.com$ [NC]
RewriteRule ^(index|about|portfolio|gallery|contact)/?$ http://www.%1/folder/$1.php?id=1 [R=302,L,NC,QSA]

一旦到位,就可以像这样创建 href 链接:

<a href="http://1.domain.com/about">About Us</a><a href="http://1.domain.com/contact">Contact Us</a>

关于php - 使用 5 个页面的 mod-Rewrite(通配符子域),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14871176/

相关文章:

php - Yii 使用 Jquery 从单选按钮列表中获取选定的值

php - 为什么需要在 php 和 AJAX 中使用 JSON

没有 .php 扩展名的 PHP URL 格式?

linux - 如何为所有请求添加 ~username 前缀?

php - 在共享主机环境中关闭页面的 mod_security

apache - 使用 apache : pass group as header to application server 进行身份验证

php - 如何使用 phpquery 查找标签名称?

php - 我可以将值从表单传递到弹出窗口,根据传递的内容显示一些内容,然后再传回一些内容吗?

.htaccess - htaccess 从根目录重定向到子文件夹,然后屏蔽 url?

.htaccess - 将所有子域 URL 重定向到主页