-1

I am looking for a way to "predict" with PHP how an url will be rewritten by my htaccess file.

Eg: www.domain.com/category/B/Nice-Article
gets rewritten to:
www.domain.com/article.php?category=B&slug=Nice-Article

This all works well. I need a PHP function that would work as follows:

function previewModRewrite(url){
//some magic stuff happens based on the rewrite rules on my server
}

If I call the function
previewModRewrite("www.domain.com/category/B/Nice-Article")
it should return:
www.domain.com/article.php?category=B&slug=Nice-Article

Just to clarify: this script will run on my server on the same domain as where the htaccess file is located.

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Digits
  • 1,311
  • 2
  • 12
  • 21
  • So, aside from writing a stub of a function, have you tried to do this? What was the result? Are you expecting the code to read all your htaccess files and apache config to parse what rules might apply? Maybe you could share the rule(s). Have a look at preg_match and preg_replace which would accept the same patterns that mod_rewrite uses to convert the url. – Jonathan Kuhn Jan 04 '16 at 23:40
  • I can program all the rules from htaccess with regular expressions, but I am looking for a way so that the code will automatically adjust itself every time the htaccess file changes. Since PHP / mod_rewrite is a very ofthen used combo, I was hoping somebody would know of a way how this might be done. The stub was just there to clarify what my goal is. Any piece of advice to point me in the right direction would be greatly appreciated. – Digits Jan 04 '16 at 23:44
  • Thing is though that there can be a lot of things you would need to parse. You would need to go from the path of the url passed in, up and up until you found an .htaccess file with `RewriteEngine on`. Adjust for `RewriteBase`, look for blocks of `RewriteCond` and `RewriteRule`, parse each condition for things like environment variables, flags (`[NC]`, `[O]`, `-f`), then parse the rule for pattern and new url, check the pattern matches the url and match then do a replace. There would be a lot of work to say the least. And they can be in httpd.conf too which you can't check.. – Jonathan Kuhn Jan 04 '16 at 23:52
  • Yes, that is why I wouldn't want do it that way. I was thinking/looking for a way to do it via linux shell and then execute that command via PHP. Soo far no luck either – Digits Jan 05 '16 at 00:00
  • The "easiest" would probably be to write a PHP extension which runs the URL through the actual rewrite module. – alexn Jan 06 '16 at 17:41

1 Answers1

1

The lack or replies probably indicated that this isn't possible as defined in the question.

The workaround I will be using will be having a PHP version of my mod_rewrite rules. So every change in the .htaccess file will need to be done in 2 places: htaccess and PHP. Not ideal, but the only workable solution soo far.

Digits
  • 1,311
  • 2
  • 12
  • 21