.htaccess和子目录

时间:2015-09-04 14:46:02

标签: regex apache .htaccess mod-rewrite

我正在尝试创建一个基本上是通配符设置的.htaccess文件。我有一个文件夹结构如下。

public_html/sites is the root directory and in this folder there are two sub directories:

public_html/sites/brand (there are many brand folders, just used brand as example)
public_html/sites/brand/event (there are many events in a brand folder, just used event as example)

I have files in each event folder such as index.php and media.php

我没有成功将媒体页面重写为我正在寻找的网址结构。下面是我当前的.htaccess文件。

RewriteEngine On
RewriteRule ^media/([^/]*)$ /media.php?unqid=$1 [L]

所以我想要的预期网址是/ testbrand / 4177 / media / 123456。我将在每个品牌下有几个品牌和几个活动,所以如果可能的话我需要某种通配符。任何帮助表示赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:2)

如果您想拥有这样的网址

http://example.com/testbrand/4177/media/123456

您的品牌是动态的,那么您的重写应该 可能看起来像这样。

RewriteRule ^(?:[^/]+)/(?:[0-9]+)/media/([0-9]+)/?$ /media.php?unqid=$1 [L]

根据您当前的内部网址,您只需将/media/之后的ID号发送给PHP。如果您需要发送更多信息,则需要更新内部URI参数。

答案 1 :(得分:0)

您可以在根目录中使用此规则.htaccess:

Server: nginx/1.8.0
Date: Fri, 04 Sep 2015 13:13:14 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 3555
Status: 200 OK
X-XHR-Current-Location: /shop/301316.json
X-UA-Compatible: IE=Edge,chrome=1
ETag: "2e88dffe16a385872368e19e0370a999"
X-Request-Id: 3404c637c6a499d8e32a6e5c243e4d69
X-Runtime: 0.065267
X-Rack-Cache: miss
Accept-Ranges: bytes
X-Varnish: 561085217 561069463
Age: 823
Via: 1.1 varnish
Cache-Control: private, max-age=0, must-revalidate
Pragma: no-cache
X-Cache: HIT
X-Cache: MISS from localhost
X-Cache-Lookup: MISS from localhost:3128
Connection: close

当您请求RewriteEngine On RewriteRule ^([^/]+/[^/]+)/media/(\d+)/?$ $1/media.php?unqid=$2 [L,NC,QSA]

时,这会加载/testbrand/4177/media.php?unqid=123456