首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mod_rewrite规则

mod_rewrite规则
EN

Stack Overflow用户
提问于 2012-07-24 19:10:49
回答 1查看 116关注 0票数 2

你好,我正在尝试设置我的URL设置,并且我已经要求了一些要求,但我的最后一个问题是:

例如,如果用户试图访问site.com/foo.php,则应该将其重定向到site.com/foo;反之,如果用户进入site.com/foo,则不应该重定向,而请求的页面应该是site.com/foo.php

这是我的.htacces:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-24 19:22:57

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

代码语言:javascript
复制
# 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]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11637724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档