htaccess的重写:子域中GET var和路径GET变种 [英] .htaccess rewrite: subdomain as GET var and path as GET var

查看:213
本文介绍了htaccess的重写:子域中GET var和路径GET变种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

期望的结果:

  http://example.com/  - >的index.php
http://www.example.com/  - >的index.php
http://hello.example.com/  - >的index.php?子域=你好
http://whatever.example.com/  - >的index.php?子域=什么
http://example.com/world  - >的index.php?路径=世界
http://example.com/world/test  - >的index.php?路径=世界/测试
http://hello.example.com/world/test  - > ?index.php的子域=你好&放大器;路径=世界/测试
 

随着htaccess的我,现在,我可以实现一个或其他重映射,但不能同时在同一时间。

  RewriteEngine叙述上

#解析子域名,因为我们可以在PHP访问的变量,
#运行的index.php脚本
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{HTTP_HOST}!^ WWW
的RewriteCond%{HTTP_HOST} ^([^ \。] +)\。([^ \。] +)\。([^ \。] +)$
重写规则^ * $的index.php?子域=%1

#地图所有请求的路径得到变量的index.php
的RewriteBase /
重写规则^指数\ .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ /index.php?path=$1 [L]
 

我有一个很难两者结合...任何指针,好吗?

修改
的不必要的行为,我遇到了现在,如果我有一个子域之后的.com /路径,只有子域名会被通过,即:

  http://hello.example.com/world->的index.php?子域=你好
 

解决方案

使用的第一个规则添加子域名参数,不改变URI,然后用第二个规则路由的URI 的index.php

  RewriteEngine叙述上

#解析子域名,因为我们可以在PHP访问的变量,
#运行的index.php脚本
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{HTTP_HOST}!^ WWW
的RewriteCond%{HTTP_HOST} ^([^ \。] +)\。([^ \。] +)\。([^ \。] +)$
重写规则^(。*)/ $ 1?子域=%1

#地图所有请求的路径得到变量的index.php
重写规则^指数\ .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ /index.php?path=$1 [L,QSA]
 

第二个规则的需要的有 QSA 标记,否则第一个规则的查询字符串会丢失。

Desired result:

http://example.com/                 -> index.php
http://www.example.com/             -> index.php
http://hello.example.com/           -> index.php?subdomain=hello
http://whatever.example.com/        -> index.php?subdomain=whatever
http://example.com/world            -> index.php?path=world
http://example.com/world/test       -> index.php?path=world/test
http://hello.example.com/world/test -> index.php?subdomain=hello&path=world/test

With the .htaccess I have right now, I can achieve one or the other re-mapping, but not both at the same time.

RewriteEngine On

# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}        !^www
RewriteCond %{HTTP_HOST}         ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^.*$ index.php?subdomain=%1

# Map all requests to the 'path' get variable in index.php
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L] 

I'm having a hard time combining the two...any pointers, please?

EDIT
The unwanted behavior I'm experiencing now is that if I have a subdomain and a path after .com/, only the subdomain will be passed through, ie:

http://hello.example.com/world-> index.php?subdomain=hello

解决方案

Use the first rule to add the subdomain parameter, without changing the URI, then use the 2nd rule to route the URI to index.php:

RewriteEngine On

# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST}        !^www
RewriteCond %{HTTP_HOST}         ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /$1?subdomain=%1

# Map all requests to the 'path' get variable in index.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA] 

The second rule needs to have the QSA flag, otherwise the first rule's query string gets lost.

这篇关于htaccess的重写:子域中GET var和路径GET变种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆