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?

3
  • Did you enable rewrite mod and restarted apache? stackoverflow.com/questions/869092/… Commented Jan 4, 2012 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. Commented Jan 4, 2012 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. Commented Jan 4, 2012 at 12:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That is what I was afraid of. Then, is there any other way to bypass it without changing every single hardcoded include() path manually?
Yes. Change the include_path setting as I have mentioned in my answer above. So long as you are using relative paths.
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.
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

1 Comment

I can't find it, but I suppose it is allowed, since rewrite works for ordinary web requests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.