使用.htaccess重写php的路径包括 [英] Using .htaccess to rewrite paths to php includes

查看:109
本文介绍了使用.htaccess重写php的路径包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是.htaccess的专家,因此,如果这个问题听起来很愚蠢,那就对不起。

I am not expert for .htaccess, therefore sorry if this question sounds stupid.

我的ISP将整个网站都移到了新服务器上。根目录路径已更改。许多文件使用php包括将哪个路径硬编码为旧路径。我想避免手动重写所有内容。我正在尝试使用.htaccess,像这样:

My ISP moved the whole website to new server. Path to root changed. Many files use php include which path is hardcoded to old path. I would like to avoid rewriting everything manually. I am trying to use .htaccess, like this:

Options +FollowSymlinks
RewriteEngine on
RewriteRule /oldpath/(.*) /newpath/$1 [NC]

但是它不起作用。我该怎么办?

But it doesn't work. What do I do wrong?

推荐答案

mod_rewrite 由网络请求激活通过Apache而PHP include()可以直接访问文件系统,而绕过Web请求。因此,.htaccess文件中的任何 RewriteRules 都不会通过调用PHP include() require()函数。

mod_rewrite is activated by web requests through Apache whereas PHP include()s are direct access to the file system, which bypasses the web request. Therefore any RewriteRules in your .htaccess file will not be executed by calling PHPs include() or require() function.

在.htaccess文件中解决此问题的方法是像这样设置PHP包含路径:

The way to work round this in your .htaccess file is to set the PHP include path like so:

php_value include_path ".:/usr/local/lib/php:/your/new/base/include/path"

有关更改配置设置的详细信息,请参见PHP手册。

See the PHP manual for more information on changing configuration settings.

这也可以在带有以下内容的引导PHP文件中更改:

This can also be changed in a bootstrap PHP file with:

ini_set('include_path', ini_get('include_path') . ':/your/new/base/include/path');



绝对路径



您别无选择但要在整个代码库中进行查找和替换。正如@Crontab所建议的那样,您应该使用基本路径的中央声明。

Absolute Paths

You have little choice but to do a find and replace across the code base. As @Crontab suggested you should then use a central declaration of the base path.

这篇关于使用.htaccess重写php的路径包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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