mod_rewrite的重定向不存在的URL [英] mod_rewrite redirect for non-existent URL's

查看:201
本文介绍了mod_rewrite的重定向不存在的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的网站,我已经接管。新用户实际上获得他们创建一个自定义页面。它是在一个比梦幻般的方式少做。目前,它实际上是生成一个命名为创建的蛞蝓URL文件,并象征性地将其链接到一个名为/主/文件夹中。很显然,我想改变这一点。我的计划是简单地把它重定向不存在的文件夹为/主/通过的.htaccess。目前,这是我的.htaccess如下:

I've got a an old website that I've taken over. New users essentially get a custom page created for them. It was done in a less than fantastic manner. Currently it actually generates a file named for the slug URL created and symbolically links it to a folder called "/main/". Obviously I want to change this. My plan was simply to have it redirect non-existant folders to "/main/" via .htaccess. Currently this is what my .htaccess looks like:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|index\.htm)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /main/ [L]

然而,这会产生一个重定向循环。我在想什么?

However this generates a redirect loop. What am I missing?

在这一点,我才意识到我应该说我希望它保持这是输入的路径。例如,如果我把 http://www.mydomain.com/test_person 应该保持该地址,但转发一切交给主文件夹如果是有道理的。

On that note, I realized I should say I want it to maintain the path that's input. For example if I put http://www.mydomain.com/test_person it should maintain that address, but forward everything to the main folder if that makes sense.

推荐答案

您会希望您的规则,不能重写与主已经开始,为了不网址有它循环,例如:

You'll want your rule to not rewrite URLs already beginning with main in order to not have it loop, eg:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^main/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+/(.*)$ /main/$1 [L]

[L]并不真正意味着什么,你会觉得[L]应该意味着.htaccess文件的情况下。

[L] doesn't really mean what you would think [L] should mean in the context of a .htaccess file.

您应该能够得到从REQUEST_URI环境变量所要求的原始地址,但一个共同的做法是到塞传给目标,作为GET变量。

You should be able to get the original url requested from the REQUEST_URI environment variable, but a common thing to do is to pass the slug to the target as a GET variable.

RewriteRule ^([^/]+)/(.*)$ /main/$2?user=$1 [QSA,L]

或者通过一切一个脚本,其中除$ P $点的URL并找到正确的内​​容返回:

Or pass everything to a single script which interprets the URL and finds the correct content to return:

RewriteRule ^([^/]+)/(.*)$ /main/index.php?user=$1&path=$2 [QSA,L]

这篇关于mod_rewrite的重定向不存在的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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