将网站上的所有页面(包括子文件夹)的mod_rewrite到单个php页面 [英] mod_rewrite for all pages (including subfolders) on a site to a single php page

查看:37
本文介绍了将网站上的所有页面(包括子文件夹)的mod_rewrite到单个php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将具有许多静态html页面的网站转换为由数据库驱动的网站.我的问题是我不想丢失Google已经索引的内容,因此我想重写要通过php脚本发送的请求,该脚本在数据库中查找内容的文件路径.我的理解是,mod_rewrite可以最好地达到此目的,不幸的是我从未使用过它,所以我有点迷茫.

I am in the process of converting my site with many static html pages to a site driven by a database. My problem is that I don't want to lose what google has already indexed, so I would like to rewrite requests to be sent through a php script which lookup the filepath for content in the database. My understanding is that a mod_rewrite would best serve this purpose, unfortunately I have never used it, so I am a bit lost.

我所拥有的:

www.domain.com/index.html
www.domain.com/page.html?var=123&flag=true
www.domain.com/folder/subfolder/
www.domain.com/folder/subfolder/index.html
www.domain.com/folder/subfolder/new/test.html
www.domain.com/folder/subfolder/new/test.html?var=123&flag=true

我想要的(我也可能需要对路径进行urlencode)(通过完整的uri也可以):

What I want (I also probably need to urlencode the path)(passing the full uri is also ok):

www.domain.com/index.php?page=/index.html OR www.domain.com/index.php?page=www.domain.com/index.html
www.domain.com/index.php?page=/page.html?var=123&flag=true
www.domain.com/index.php?page=/folder/subfolder/
www.domain.com/index.php?page=/folder/subfolder/index.html
www.domain.com/index.php?page=/folder/subfolder/new/test.html
www.domain.com/index.php?page=/folder/subfolder/new/test.html?var=123&flag=true

这是我的第一次尝试:

RewriteEngine On  # Turn on rewriting    
RewriteCond %{REQUEST_URI} .* # Do I even need this?    
^(.*)$ /index.php?page=$1

想法?在此先感谢:)

因此,我尝试实现Ragnar的解决方案,但是当我使用'RewriteCond $ 1'或在最后一行包含'/'时,仍然出现500个错误.我已经设置了一个test.php文件,该文件将回显GET _ ["page"],因此我知道重写工作正常.到目前为止,我可以获得一些正确的输出(但仅当我不在root时),例如:

So I tried implementing Ragnar's solution, but I kept getting 500 errors when I use 'RewriteCond $1' or include the '/' on the last line. I have setup a test.php file which will echo GET_["page"] so I know that the rewrite is working correctly. So far I can get some of the correct output (but only when I am not in root), for example:

RewriteEngine on
RewriteRule ^page/(.*)$ test.php?page=$1 [L]

如果我访问页面 http://www.domain .com/page/test/subdirectory/page.html?var = 123 ,它将输出"test/subdirectory/page.html"(缺少查询字符串,我需要).但是,如果我使用以下示例:

If I visit the page http://www.domain.com/page/test/subdirectory/page.html?var=123 it will output 'test/subdirectory/page.html' (missing the querystring, which I need). However, if I use this example:

RewriteEngine on
RewriteRule ^(.*)$ test.php?page=$1 [L]

如果我访问 http://www.domain.com /page/test/subdirectory/page.html?var=123 ,它只会输出"test.php",这非常令人困惑.有想法吗?

If I visit http://www.domain.com/page/test/subdirectory/page.html?var=123 it will only output 'test.php' which is thoroughly confusing. Thoughts?

编辑#2:

看来我一直在解决所有这些错误.我只是想要在我的PHP脚本页面中使用完整uri的功能.完成我想要做的工作的最终解决方案如下:

It seems I've been going about this all wrong. I just wanted the ability to use full uri in my php script page. The final working solution to do what I want is the following:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test.php

然后在我的PHP脚本中,我可以使用$ _SERVER ['REQUEST_URI']来获取所需的信息.我知道这应该比我尝试的要容易...

Then in my php script, I can use $_SERVER['REQUEST_URI'] to get what I need. I knew this should have been easier than what I was trying...

推荐答案

不需要那么多行,只会使事情复杂化.

There's no need for so many lines, it only complicates things.

您需要的是.htaccess中的2行:

rewriteengine on
#rewriterule-: ar1 path =relative. ar2 if relative, that to rewritebase.
rewriterule !^foo/bar/index\.php$ /foo/bar/index.php
#..assert ar1 dismatches correct url


在PHP中

您可以使用以下命令在PHP中输出重写器的第一个输入:

You can output the first input of rewriterule in PHP using:

<?=$_SERVER['REQUEST_URI'];

这将赋予您所有力量,让您可以做所有事情.只需手动解析$_SERVER["REQUEST_URI"],您就可以echo完全不同的页面,具体取决于$_SERVER["REQUEST_URI"]的值.

That will give you all the power and allow you to do all things. Simply parse $_SERVER["REQUEST_URI"] manually and you can echo totally different pages depending on the value of $_SERVER["REQUEST_URI"].

安全漏洞

请注意,您的服务器在重写之前可能会进行路径或错误路径. (如果没有服务器特权,则无法覆盖此行为.)例如,如果用户访问/foo////,则可能只会看到/foo//foo.例如,如果用户访问///foo,则您可能只会看到/foo.例如,如果用户访问/a/../foo,则您可能只会看到/foo.例如,如果用户访问/a//b/../../foo,则您可能只会看到/foo/a/foo [因为错误的生产服务器在..的上下文中将多个/视为不同的,没有在开玩笑]] .


Note that your server may do pathing or buggy pathing before rewriterule. (You can't override this behavior without server privileges.) Eg if the user visits /foo//// you may only see /foo/ or /foo. And eg if the user visits ///foo you may only see /foo. And eg if the user visits /a/../foo you may only see /foo. And eg if the user visits /a//b/../../foo you may only see /foo or /a/foo [because buggy production servers treat multiple / as distinct in the context of .., no kidding].


有电路中断

cin上的重写电路中断与htaccess的相对解释重写器arg2相同. (首先,我个人将禁用电路中断以降低规则的复杂性,但似乎没有办法这样做.)

Rewrite circuit breaks on cin identical to htaccess∙parentfolder∙relative interpreted rewriterule∙arg2. (First off, personally I'd disable circuit breaks to reduce rule complexity but there seems to be no way to do so.)

断路解决方案:

rewriteengine on
#rewriterule-: ar1 path =relative. ar2 if relative, that to rewritebase.
rewriterule ^ /foo/bar/index.php
#..circuit-breaking, so assert ar2 if absolute, htaccess parentfolder =root, else, htaccess parentfolder not in interpreted ar2.


电路中断和重写库的非设计用途

断路需要以下任一条件:

Circuit break needs either of:

  1. [重写器]的arg2⇌绝对.和htaccess父文件夹⇌根.
  2. arg2⇌相对的.而且该文件夹不在arg2中.

因此,当该文件夹≠根,断路需要arg2⇌相对的.当arg2⇌时相对而言,断路需要该文件夹⇌不在解释的arg2⏋中.

So when that folder ≠ root, circuit break needs arg2 ⇌ relative. when arg2 ⇌ relative, circuit break needs⎾that folder ⇌ not in interpreted arg2⏋.

说我们需要断路和解释为arg2的htaccess父文件夹,因此我们通过rewritebase编辑arg2:

Say we need circuit break and a htaccess parentfolder that's in interpreted arg2, so we edit arg2 via rewritebase:

rewriteengine on
#rewriterule-: ar1 path =relative. ar2 if relative, that to rewritebase.
rewriterule ^ bar/index.php
#..circuit-breaking, so assert ar2 if absolute, htaccess parentfolder =root, else, htaccess parentfolder not in interpreted ar2.
rewritebase /foo
#..needs to be absolute [</> starting]. pathing [eg </../a/..> and </../..///ran/dom-f/oobar/./././../../..////////.//.>] allowed

这篇关于将网站上的所有页面(包括子文件夹)的mod_rewrite到单个php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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