How to stop direct php page execution using htaccess rules?

In my .htaccess file, I defined the following rule to make the URL of my registration page http://example.com/register/

RewriteRule register/ /register.php

The above rule is great, but I can access my registration page from http://example.com/register/as well as from http://example.com/register.php.

I don't want this user to be able to access the url from the http://example.com/register.phpurl, is there any rule that I can define in .htaccess to stop the register.php url from running or just redirect any direct register. php request / registration /

+3
source share
6 answers

, , "register.php" . , "" , , , , -, . , , index.php .htaccess, www-root:

RewriteEngine on
RewriteRule ^(.*?)$ index.php?file=$1

index.php , / , $_GET [ "file" ]. 100% , register.php, ( ) .htaccess :

DENY from all 

. define() - index.php register.php

defined('access') or die('Intruder alert!');

. , :

header("Status: 301"); /* Content moved permanently */
header("Location: http://yourserver/Register/");
exit;

, , $_SERVER["REQUEST_URI"], - ".php" , , .

+4

, , .htaccess .

, , :

Options -Indexes
order   allow,deny
deny    from all

() , .

+3

URL- URL-, . :

RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php[/?\s]
RewriteRule (.+)\.php$ /$1 [L,R=301]

( ):

RewriteRule ^register/$ register.php
+1

.

RewriteCond %{REQUEST_FILENAME} ^register\.php$ [NC]
RewriteRule ^/register register.php

Redirect register.php /register
0

, rel= .

, , 301 /register.php /register/

register.php

if ( stristr( $_SERVER['REQUEST_URI'], '.php' ) )
{
    header ('HTTP/1.1 301 Moved Permanently');
    header ('Location: /register');
}
0

/register.php mod_rewrite, SleepyCod:

RewriteCond %{REQUEST_FILENAME} register\.php [NC]
RewriteCond %{IS_SUBREQ} false
RewriteRule .* - [F,L]

:

  • [NC]: , , .
  • 1: - "register.php"
  • 2: ( , RewriteRules ).
  • :
  • Flags:: [F]send forbidden header 403,: [L]this is the last rule to apply, skip all the following rewrite rules

Rewriting correctly is an art in itself. I suggest you carefully read http://httpd.apache.org/docs/2.2/rewrite/ .

Greetings

0
source

Source: https://habr.com/ru/post/1712169/


All Articles