gpt4 book ai didi

php - mod_rewrite 规则

转载 作者:行者123 更新时间:2023-11-28 00:16:59 25 4
gpt4 key购买 nike

您好,我正在尝试设置我的 URL 设置,并且我已经提出了一些要求,但我的最后一个问题如下:

例如,如果用户尝试访问 site.com/foo.php,他们应该被重定向到 site.com/foo;相反,如果用户输入 site.com/foo,他们不应被重定向,但请求的页面应该是 site.com/foo.php

这是我的 .htacces:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^.*\.html$ %1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

AddType application/x-httpd-php .htm .html

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

Redirect /anmeldung http://www.site.com/register
Redirect /anmeldung.htm http://www.site.com/register
Redirect /anmeldung.html http://www.site.com/register
Redirect /anmeldung.php http://www.site.com/register

Redirect /registierung http://www.site.com/register
Redirect /registierung.htm http://www.site.com/register
Redirect /registierung.html http://www.site.com/register
Redirect /registierung.php http://www.site.com/register

Redirect /register.htm http://www.site.com/register
Redirect /register.html http://www.site.com/register

如果有人可以帮助我,我将不胜感激。非常感谢。

更新:

完整的 htaccess:

RewriteEngine On

EewriteRule ^(.*)\.(php|html)$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]

Redirect /anmeldung http://www.site.com/register
Redirect /anmeldung.htm http://www.site.com/register
Redirect /anmeldung.html http://www.site.com/register
Redirect /anmeldung.php http://www.site.com/register
Redirect /registierung http://www.site.com/register
Redirect /registierung.htm http://www.site.com/register
Redirect /registierung.html http://www.site.com/register
Redirect /registierung.php http://www.site.com/register

最佳答案

根据我上面的假设评论,你应该尝试

# This rewrites all incoming request to *.php or *.html to remove the extension. NOte the R=301 which give header indicating to the browser/spider that the resource has been relocated. The L indicates this is the last rule to be processed before sending headers to browser (which will start the process all over again) This rule MUST be first
RewriteRule ^(.*)\.(php|html)$ /$1 [R=301,L]

#This set of conditions is only applicable if the request is not a directory, is not itself a file, and has a similarly name file with .php extension. Here we do not perform a URL rewrite (i.e. [R]), but just pass this to the .php file as the last htaccess rule executed.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]

关于php - mod_rewrite 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11637724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com