【问题标题】:Redirect url with index.php to non index.php AND character underscore to dashes (.htaccess)将带有 index.php 的 url 重定向到非 index.php 和字符下划线到破折号 (.htaccess)
【发布时间】:2018-02-15 06:24:51
【问题描述】:

默认情况下。 Codeigniter url 在每个 url 中都有 index.php,然后使用下划线。

我现在的配置。

在 Routes.php 中我设置了 $route['translate_uri_dashes'] = TRUE; 。这意味着当我尝试输入 example.some_path 时,它将转到 example.some-path

在我的 config.php 中。

$config['index_page'] = '';// remove'index.php';

我的 .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

# Don't listing directory
Options -Indexes
ErrorDocument 403 http://example/404

# Origin setting
SetEnvIf Origin "http(s)?://(.+\.)?example\.com(:\d{1,5})?$" CORS=$0

Header set Access-Control-Allow-Origin "%{CORS}e" env=CORS
Header merge  Vary "Origin"

现在的问题。

1) 是的,example.com 或 example.com/somepath(没有 index.php)正在工作。但是 example.com/index.php 或 example.com/index.php/somepath 也仍然可用并且也可以工作。即使我没有 robots.txt 的索引,当访问者访问它时也会出现重复的内容错误。那么如何确保无法访问 index.php 的任何内容?使用 .htaccess?

2) 设置 $route['translate_uri_dashes'] = TRUE; 后,所有带下划线的链接都会自动转换为破折号(并在我点击包含下划线的网站中的链接时自动插入 index.php)..(例如:一旦我点击链接 <a href="http://example.com/some_path">my link</a> 它将转换为 example.com/index.php/some-path)。当我尝试直接访问 example.com/some-path 而不单击我网站上的任何链接时。是的,example.com/some-path 工作。

如何确保 index.php 无法访问并自动重定向到非 index.php?

【问题讨论】:

    标签: php apache .htaccess codeigniter mod-rewrite


    【解决方案1】:

    您可以像我一样尝试使用 web.config。我不太喜欢 .htaccess 可能是这样的:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Imported Rule 1" stopProcessing="true">
              <match url="^(.*)$" ignoreCase="false" />
              <conditions logicalGrouping="MatchAll">
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              </conditions>
              <action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
              </rule>
          </rules>
        </rewrite>
            <directoryBrowse enabled="true" />
      </system.webServer>
    </configuration>
    

    【讨论】:

    • 什么 是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多