StilgarBF
StilgarBF

Reputation: 1060

rewrite path to get parameter

I need any call to a html-file to be changed to a php and the full path and file should be passed on as a get-parameter.

for example

http://www.domain.com/any/path/file.html

should become

http://www.domain.com/index.php?path=any-path-file.html

I want to route every call to a html-file through the php file. The rewriting should ignore any other files.

In addition: would it be possible to add other parameters again?

/file.html?foo=bar
becomes
/index.php?path=file.html&foo=bar

thanks in advance Stil

Upvotes: 1

Views: 916

Answers (1)

Dom
Dom

Reputation: 3100

I don't think its possible to do exactly what you are trying to do, but I think we can get so close it wont matter.

The following rewrite

RewriteCond %{REQUEST_URI} \.html$
RewriteRule (.*) /index.php?path=$1&%{QUERY_STRING} [L]

will take any request that ends with .html and rewrite it to index.php, adding the complete path as a query string. It will also append any existing query string on the original request to the new request. This doesn't do quite what you asked for as the path variable will look like path=any/path/file.html, not path=any-path-file.html as you asked for. But this is nothing which can't just be sorted out in the first few lines of of index.php with

    if( isset($_GET['path']) ) {
        $_GET['path'] = str_replace("/","-",$_GET['path']);
    }

Upvotes: 2

Related Questions