0

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

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?

shanethehat
  • 15,105
  • 10
  • 54
  • 84
cincplug
  • 954
  • 4
  • 15
  • 33
  • Did you enable rewrite mod and restarted apache? http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2 – ngen Jan 04 '12 at 12:34
  • @ngen: mod_rewrite is not the answer. And this is a very good reason why you should never hardcode paths in web apps! If you need path information, set a variable in one common include file and include it with a relative path in every other script you have. – Crontab Jan 04 '12 at 12:39
  • Your example is too fictional to be answered conclusively. Your only poption is to rewrite the code or request a symlink from `/oldpath` to `/newpath` if it's really absolute paths. – mario Jan 04 '12 at 12:40

2 Answers2

1

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.


Relative paths

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"

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

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

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

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.

Treffynnon
  • 20,415
  • 5
  • 59
  • 95
  • That is what I was afraid of. Then, is there any other way to bypass it without changing every single hardcoded include() path manually? – cincplug Jan 04 '12 at 12:38
  • Yes. Change the `include_path` setting as I have mentioned in my answer above. So long as you are using relative paths. – Treffynnon Jan 04 '12 at 12:39
  • Well it just throws 500 Internal server error. Looks I'm going to just rewrite them by hand. Maybe that is better too. Thanks everyone. – cincplug Jan 04 '12 at 13:02
0

Have you checked if your Apache allows .htaccess to override your default config? You should have something like AllowOverride All in the root of your http docs folder or wherever you need it

jere
  • 4,146
  • 1
  • 25
  • 38