Question

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

Was it helpful?

Solution

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 )

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top