简体   繁体   English

使用index.php将网址重定向到非index.php并将字符下划线重定向到破折号(.htaccess)

[英]Redirect url with index.php to non index.php AND character underscore to dashes (.htaccess)

In default. 在默认情况下。 Codeigniter url have index.php in every url then using underscore. Codeigniter网址的每个网址中都有index.php,然后使用下划线。

My configuration righ now. 现在是我的配置。

in Routes.php I've set $route['translate_uri_dashes'] = TRUE; 在Routes.php中,我已将$route['translate_uri_dashes'] = TRUE;设置$route['translate_uri_dashes'] = TRUE; . That's mean when i try to enter example.some_path it will be go to example.some-path 这意味着当我尝试输入example.some_path时,它将转到example.some-path

in my congfig.php. 在我的congfig.php中。

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

My .htaccess 我的.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"

Issues right now. 现在发行。

1) Yes, example.com or example.com/somepath (without index.php) is working. 1)是,example.com或example.com/somepath(不带index.php)正在运行。 BUT example.com/index.php or example.com/index.php/somepath also still available and working too. 但是example.com/index.php或example.com/index.php/somepath仍然可用,并且也可以正常工作。 It will be duplicate content error when visitor access it even I've no-index with robots.txt. 访客访问它时,即使我的robots.txt没有索引,也会出现重复的内容错误。 So how to maksesure anyting with index.php not accessible? 那么如何无法访问index.php呢? with .htaccess? 与.htaccess?

2) After set $route['translate_uri_dashes'] = TRUE; 2)设置$route['translate_uri_dashes'] = TRUE; , all link with underscores automatically translate to dashes (AND automatically insert index.php once I click link in my site that's contains underscore) .. (ex: once I click link <a href="http://example.com/some_path">my link</a> it will translate to example.com/index.php/some-path). ,所有带有下划线的链接会自动转换为破折号(一旦我单击包含下划线的网站中的链接,就会自动插入index.php)。(例如:单击链接<a href="http://example.com/some_path">my link</a> ,它将转换为example.com/index.php/some-path)。 When I try visit example.com/some-path directly without click any link form my site. 当我尝试直接访问example.com/some-path时,无需单击我网站上的任何链接。 Yes, example.com/some-path working. 是的,example.com / some-path可以正常工作。

How to makesure index.php can't accessible and automatically redirect to non index.php? 如何确保index.php无法访问并自动重定向到非index.php?

You could try using web.config like I do. 您可以像我一样尝试使用web.config。 I'm not much of a fan of .htaccess Maybe something like: 我不太喜欢.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>

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

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