简体   繁体   中英

Remove .php from example.com/test.php/foo/bar

I'm looking for a way to simply remove ".php" from a url while ignoring everything after the .php

So example.com/test.php/foo/bar would become example.com/test/foo/bar

and then in the PHP template I can search for and utilize foo or bar as I please. Everything I've found to remove .php interferes with the variables at the end.

UPDATE : I've found some success by removing .php from the file name and then using ForceType . Although It'd still be nice if I could keep the .php extension so my Code Editor knows how to highlight the syntax :)

<FilesMatch "^test$">
    ForceType application/x-httpd-php5
</FilesMatch>

Update 2 Here's a RewriteRule that I've tried using to some success, but when I add a trailing slash and some content after, it results in a 500 internal server error

RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
    RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
    RewriteRule (.*)\.php$ $1 [R=301]
# remove index
    RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]

Your last block is almost there. However you need a bigger assortment of RewriteCond s to make it work with an optional PATH_INFO.

Namely you need to match the word characters from the REQUEST_FILENAME/_URI first, and then test for existence of a like-named .php script:

RewriteCond %{REQUEST_URI}           ^/(w+)/
RewriteCond %{DOCUMENT_ROOT}/%1.php  -f
RewriteRule ^(\w+)/(.+)$ $1.php/$2   [L]

The last RewriteRule depends on existing trailing vars. Add one of the generic rule blocks for plain http://example.com/test requests without PATH_INFO. Note that this specific set will work with xyz.php scripts in the webroot only.

Please try to recheck this:

RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) ^/%1.php/%2/%3

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