Connor
Connor

Reputation: 15

htaccess rewrite first directory as filename

I was wondering how I would perform a htaccess rewrite, as explained below.

domain.com/filename/testing123 --> domain.com/filename.php?/testing123

Where "filename" is the name of a php file. This "filename" can change, and anything after the "filename" is added after the "?".

Another example would be:

domain.com/abc/other/information/etc --> domain.com/abc.php?/other/information/etc

Thanks,

Connor

Upvotes: 1

Views: 965

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270677

Use ^([^/]+) to match everything up to the first /, onto which .php will be appended, and (.*) to match everything else which will be appended after ? via $2.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(.*)$ $1.php?$2 [L]

Upvotes: 1

Related Questions