1

I have probably a rather simple mod_rewrite question, however I didn't know how what to google for and how to do this in the best way.

In my php/html code I have regular links linking to /directory/file.php

However the /directory/ doesn't actually exist, and the file.php lies in the root of the server. I just want the url to make more sense if I add /directory/ to it.

How would I do that?

1 Answer 1

1

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^directory/(.+?\.php)$ /$1 [L,NC]

This will internally route a request of /directory/abcd.php to /abcd.php.

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

2 Comments

Thank you, is it possible that in this case the paths inside the file.php are no longer valid? E.g. stylesheets are not loaded properly, etc.
That is due to your use of relative links in css/js paths. To fix that issue try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current page's URL.

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.