繁体   English   中英

URL 使用 htacces 重写/重定向不起作用

[英]URL Rewrite/Redirection Not Working Using htacces

我在 Apache/2.4.25 (Debian) 服务器上,我正在托管一个包含 5 个 php static 页面的站点。

我希望网址永久显示为example.com/page1/而不是example.com/page1.php

还想在访问example.com/page1.php时显示404 error message

我正在使用下面的代码并收到404 error

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^about/?$ about.php
    RewriteRule ^privacy-policy/?$ privacy-policy.php
    RewriteRule ^contact/?$ contact.php
</IfModule>

请帮我解决问题。

您提供的代码对于删除文件扩展名是正确的。 您想要的可以通过以下方式实现:-

重定向到自定义页面:

<IfModule mod_rewrite.c>
# Run Php without filename extension
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php

    # Return 404 if original request is .php
    RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
    RewriteRule .* new.php
</IfModule>

重定向到默认页面:

<IfModule mod_rewrite.c>
    # Run Php without filename extension
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php

    # Return 404 if original request is .php
    RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
    RewriteRule .* - [L,R=404]
</IfModule>

希望这可以帮助:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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