简体   繁体   English

使用多个参数进行Mod重写

[英]Mod Rewrite with multiple parameters

I'm trying to setup a rewrite rule so that any text in place of a subdomain will be parameter one and any text after the first forward slash will be parameter two but I'm struggling with regex and am unsure about rewrite terminology. 我正在尝试设置一个重写规则,以使代替子域的任何文本都将成为参数1,而第一个正斜杠之后的任何文本都将成为参数2,但是我在正则表达式方面苦苦挣扎,并且不确定重写术语。

For example, if someone requested: 例如,如果有人要求:

joebloggs.mydomain.com

I would like them to see: 我希望他们看到:

mydomain.com/index.php?site=joebloggs

Also, if someone requested: 另外,如果有人要求:

joebloggs.mydomain.com/contact

I would like them to see: 我希望他们看到:

mydomain.com/index.php?site=joebloggs&page=contact

By "see" I mean see the page, rather than see the URL - it's a CMS-like project so you can probably see where I'm going with it. “看到”是指看到页面,而不是看到URL-这是一个类似CMS的项目,因此您可能会看到我要处理的内容。 Also, I've worked out how to remove www. 此外,我已经研究出如何删除www。 so that's not an issue :) 所以这不是问题:)


EDIT 编辑

Current .htaccess is: 当前的.htaccess是:

RewriteEngine On

# Remove trailing slash
RewriteRule ^(.+)/$ $1 [L]

# Remove www
RewriteCond %{HTTP_HOST} ^www.richardmjenkins.com$ [NC]
RewriteRule ^(.*)$ http://richardmjenkins.com/$1 [R=301,L]

# Rewrite for site/page pair
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^(.+/)?([^/]*)$ p.php?s=%1&p=$2 [QSA,L,NC]

RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^$ p.php?s=%1 [QSA,L,NC]

1. In your host panel : add a subdomain with name : * for enable all subdomains 1.在主机面板中:添加名称为*的子域以启用所有子域

2. this is your htaccess code : 2.这是您的htaccess代码:

RewriteEngine On
Options +Followsymlinks

RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^$ index.php?site=%1 [QSA,L,NC]

RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?site=%1&page=$1 [QSA,L,NC]

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory: 通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在DOCUMENT_ROOT目录下的.htaccess

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

RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^$ index.php?site=%1 [L,QSA]

RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^(.+)$ index.php?site=%1&page=%2 [L,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM