Domanda

I want to require a PHP File with a get parameter if and only if someone request a URL without any extension like .php and .html etc (i.e. directory) and that directory also don't exist.

For Example : example.com/xyz now there is no xyz directory on server then .htaccess requires a php file with get parameter - myfile.php?url=xyz

È stato utile?

Soluzione

but if i request xyz?id=1 it still send only xyz. Is there any way to send whole string no matter what is string

As you mentioned if you request example.com/xyz or example.com/xyz?id=1 you want to get all the parameters. Then use this. Notice the QSA flag. That will append the requested query string. I think this is the answer you're looking for.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myfile.php?url=$1 [QSA,L]

So if you request this example.com/xyz?id=1&h=2

Your PHP print_r($_GET); output will be this.

Array ( [url] => xyz [id] => 1 [h] => 2 )

Altri suggerimenti

start with this snippet

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ myfile.php?url=$1 [PT,L]
    ErrorDocument 404 /myfile.php
</IfModule>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top