繁体   English   中英

在以下情况下如何重写URL?

[英]How do I URL Rewrite for below situation?

我在以下网址处有一个网页。

https://example.com/contact.php

现在我要配置URL重定向之类的。 如果用户打开的页面具有完整的URL,则应自动从URL中删除.php。

https://example.com/contact.php ==> https://example.com/contact/

现在, https: //example.com/contact/ URL应该从/contact.php加载内容。 我正在.htaccess下尝试,但由于重定向循环而失败。

RewriteEngine on

RewriteRule ^contact$ contact.php  [NC,L]
RewriteCond %{REQUEST_URI} !^contact.php$
RewriteRule ^contact.php$ http://example.com/contact/  [R=301]

使用以下代码删除扩展

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(one)/?$ /$1.php [L,NC]

请尝试以下操作:

# It redirects the contact.php to /contact/, so .php extension is removed
RewriteRule ^contact\.php$ /contact/ [L,R=301]

# It fetches contact.php when /contact/ is requested by the user
RewriteRule ^/contact/$ /contact.php [L,NC]

暂无
暂无

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

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