mod_rewrite多个变量和子文件夹 [英] mod_rewrite Multiple Variables and Subfolders

查看:35
本文介绍了mod_rewrite多个变量和子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我有多个这样的网址:

On my site I have multiple URLs like this:

主页:

mysite.com
mysite.com/?content=about
mysite.com/?content=posts&page=2

子文件夹:

mysite.com/subsite/
mysite.com/subsite/?content=about
mysite.com/subsite2/?content=posts&page=2

我想把这些清理干净:

mysite.com/about/
mysite.com/posts/2

mysite.com/subsite/about/
mysite.com/subsite/posts/2

现在,我已经可以对一个变量和其他一些简单的东西使用mod_rewrite了,但是我不确定如何实现.当我使用时:

Now, I've been able to use mod_rewrite for one variable, and some other simple things, but I'm not exactly sure how to accomplish this. When I use:

RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /?content=$1&page=$2 [L]

它将URL的各个部分识别为变量,但也将子站点视为变量,并尝试为内容"插入子站点".

It recognizes the sections of the URL as variables, but it also sees the subsite as a variable, and attempts to plug in 'subsite' for 'content'.

有人能指出我正确的方向吗?

Would anyone be able to point me in the right direction?

推荐答案

我最终通过在subsite文件夹中放置了另一个.htaccess文件来使它正常工作.

I ended up getting this to work by putting another .htaccess file in the subsite folder.

.htaccess具有:

RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]


在子站点.htaccess中,我具有:(与根.htaccess相同)


In the subsite .htaccess I have: (same as root .htaccess)

RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]

如果不需要更改子目录的url,则仅将RewriteEngine On添加到子站点的.htaccess.这基本上会覆盖根.htaccess的重写规则,并从子文件夹加载页面.

If you do not need to alter the subdirectory's url, add only RewriteEngine On to the subsite's .htaccess. This basically overrides the root .htaccess rewrite rules, and loads the page from the subfolder.

这篇关于mod_rewrite多个变量和子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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