Using mod_rewrite to remove PATH_INFO from URL

UPDATE: Mostly solved. Now it only doesn't work if in the situation where you are trying to access http://www.example.com/any-real-file-without-an-extension/anything-afterwards-that-doesnt-end-in-a-slash. I guess the server thinks that any-real-file-without-an-extension might be a directory... not sure how to fix that.

PHP defines "PATH_INFO" as a part of the URL that:

Contains any client-provided pathname information trailing the actual script filename but preceding the query string, if available. For instance, if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.

As of right now, my website treats http://example.com/ contact_us.php/ANYTEXTCANGOHERE as http://example.com/contact_us.php

In this case, /ANYTEXTCANGOHERE is PATH_INFO. I would like to make it so that either this path info is deleted up to the filename, or otherwise a 404 is returned. Is there any way that I can do that?

Here is what my .htaccess looks like now. It's pretty simple. Just a 301 redirect to www and a stripslashes at the end, as well as something to force association to files without extensions and a rewrite without extensions. I do not have server root access.

ErrorDocument 404 /404.php
ErrorDocument 503 /503.php

#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^foodthing.org [NC]
RewriteRule ^(.*)$ http://www.foodthing.org/$1 [L,R=301,NC]

#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?foodthing\.org$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

#1)externally redirect "/file.php" to "/file"   
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
1
задан 24 August 2017 в 17:34
1 ответ

Чтобы отключить дополнительную информацию о пути, вам просто нужно отключить AcceptPathInfo в конфигурации сервера (или файл .htaccess ). Например:

AcceptPathInfo Off

Любой URL-адрес, содержащий информацию о пути, теперь будет вызывать 404.

1
ответ дан 3 December 2019 в 23:26

Теги

Похожие вопросы