简体   繁体   中英

Optional Subdirectory Internal Redirect, Htaccess

I currently use this code to silently call another php file by using the file name as a get parameter:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  blog/([^/]+)/?$  [NC]
RewriteRule .*   repost_p.php?post=%1  [L]

what if I wanted to make the file name a fake subdirectory, and add a file name as a second get parameter?

how would I go about doing that?

You could just use something like:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/blog/(.*)   /repost_p.php?path=$1  [L]

Then in the repost_p.php file you can filter the path variable to find out the subdirectories.

$path = isset($_GET['path']) ? $_GET['path'] ? '';
$params = explode('/', $path);

//params is now an array with all subdirectories.
//Make sure you validate each one for valid input! This can also be used for sql injection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM