4L3X
4L3X

Reputation: 115

How to rewrite URL paths to query strings

Here is my .htaccess file:

# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]

# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# To check whether the PHP file exists and set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]

# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

Assume there is a file.php in the root directory and v1,v2,... are my query string inputs and the URL below is requested:

http://domain.tld/file/v1/v2...

I simply need to send v1,v2,... as HTTP GET to the file.php using .htaccess and it should work with any other PHP file names and in case of possibility any number of inputs, otherwise handling up to 3 inputs is enough.

Upvotes: 1

Views: 42

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

I assume that file.php is just a template name, and is not limited to that particular name. You can try:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/(.+)$ $1.php?params=$2 [NC,L]

Now, in the file.php, you will get the passed query arguments as:

$_GET['params']  // which will be of the form v1/v2/v3...

Upvotes: 1

Related Questions