.htaccess 如果用户请求direct.php文件,则重定向该用户

hjzp0vay  于 2022-11-16  发布在  PHP
关注(0)|答案(1)|浏览(108)

使用.htaccess文件,如果用户直接在浏览器中请求.php文件,我希望用户被定向到另一个URL
例如:如果用户请求了example.com/test.php,则应该将他重定向到example.com/redirectionEndpoint。
下面是我正在使用的Apache服务器配置

<VirtualHost *:80>
    ServerName app01
    DocumentRoot /var/www/public_html/app01
    <Directory /var/www/public_html/app01>
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    CustomLog /var/log/httpd/dev01-access.log common
    ErrorLog /var/log/httpd/dev01-error.log
</VirtualHost>

这是我的.htaccess文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . /index.php [L]
n6lpvg4x

n6lpvg4x1#

请使用显示示例尝试以下.htaccess规则文件请确保在测试URL之前清除浏览器缓存

RewriteEngine ON
##External redirect from test.php to redirectionEndpoint.
RewriteCond %{THE_REQUEST} \s/test\.php\?(?:\S+)?\s [NC]
RewriteRule ^ /redirectionEndpoint? [R=301,L]

##Internal rewrite rules to rewrite to test.php.
RewriteRule ^redirectionEndpoint/?$ test.php [NC,L]

##For any other non-existing page should go to index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

相关问题