简体   繁体   English

HTACCESS 规则重定向错误

[英]HTACCESS rules redirection error

In my htaccess file I have added a rule to redirect url/abc/xyz to url/abc/xyz.php so now even when I try to access url/abc.php it redirects me to url/abc/在我的 htaccess 文件中,我添加了一条规则将 url/abc/xyz 重定向到 url/abc/xyz.php 所以现在即使我尝试访问 url/abc.php 它也会将我重定向到 url/abc/

Help me with this.. My code now :帮我解决这个..我现在的代码:

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]

## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]

What I want is :我想要的是:

url/abc.php & url/abc should goto url/abc.php url/abc.php & url/abc 应该转到 url/abc.php

url/abc.php/#x & url/abc/#x should goto url/abc.php/#x url/abc.php/#x & url/abc/#x 应该转到 url/abc.php/#x

If I've interpreted your intentions correctly, the following rewrite rules should serve your wishes:如果我正确解释了您的意图,以下重写规则应该符合您的意愿:

# Enable rewriting
RewriteEngine On

# Define site root
RewriteBase /

# Do not rewrite for image, css or js files
RewriteRule \.(css|jpe?g|gif|png|js)$ - [L]

# Add index.php to requests ending with a trailing slash
RewriteRule ^([a-z0-9_\-/]*/)?$ $1index.php [NC,R=301,L]

# Add .php to the end of a request without a trailing slash
RewriteRule ^(([a-z0-9_\-]+/)*[a-z0-9\-]+)$ $1.php [NC,R=301,L]

What the rules do:规则的作用:

  • Rewriting happens relatively to the root (/) of the domain相对于域的根 (/) 进行重写
  • Rewrite rules will ignore any file with a file extension listed in rule nr 2.重写规则将忽略具有规则 nr 2 中列出的文件扩展名的任何文件。
  • Requests for http://example.com/ (or http://example.com ), http://example.com/foo/ and http://example.com/foo/bar/ will be rewritten to http://example.com/index.php , http://example.com/foo/index.php and http://example.com/foo/bar/index.php respectivelyhttp://example.com/ (或http://example.com )、 http://example.com/index.php http://example.com/foo/http://example.com/foo/bar/请求将被重写为http://example.com/index.php , http://example.com/foo/index.phphttp://example.com/foo/bar/index.php分别
  • Requests for http://example.com/baz and http://example.com/baz/boo will be rewritten to http://example.com/baz.php and http://example.com/baz/boo.php respectivelyhttp://example.com/bazhttp://example.com/baz/boo请求将被重写为http://example.com/baz.phphttp://example.com/baz/boo.php分别

The URL fragment (#) is never sent to the server so you cannot use mod_rewrite to redirect it. URL 片段 (#) 永远不会发送到服务器,因此您不能使用 mod_rewrite 重定向它。 In other words the # is only interpreted in the browser so you shouldn't need to worry about it in .htaccess.换句话说,# 只在浏览器中解释,所以你不需要在 .htaccess 中担心它。

This code should work for you, or at least get you started:这段代码应该适合你,或者至少让你开始:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
</IfModule>

I have also enclosed it in an IfModule to check to see if mod_rewrite is enabled first.我还将它包含在一个 IfModule 中,以检查是否首先启用了 mod_rewrite。

My preferred way, however, is to redirect everything to index.php and then have PHP code there that will break up the URLs and redirect to the appropriate pages.然而,我的首选方法是将所有内容重定向到 index.php,然后在那里放置 PHP 代码,这些代码将分解 URL 并重定向到适当的页面。 This works really well with Model-View-Controller (MVC) systems and other similar variants.这对于模型-视图-控制器 (MVC) 系统和其他类似的变体非常有效。 This also allows general site settings to all be loaded from one place rather than many places.这也允许从一个地方而不是多个地方加载一般站点设置。 Here is the code that I would use for this type of setup:这是我将用于此类设置的代码:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !^/img/[0-9a-zA-Z_\-]+\.(gif|jpg|png)$
  RewriteCond %{REQUEST_URI} !^/css/[0-9a-zA-Z_\-]+\.css$
  RewriteCond %{REQUEST_URI} !^/js/[0-9a-zA-Z_\-]+\.js$
  RewriteRule ^.+$ index.php [L]
</IfModule>

The first three lines says to not redirect any images, css, or js files in the specified directories.前三行表示不重定向指定目录中的任何图像、css 或 js 文件。

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

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