简体   繁体   中英

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.

My configuration righ now.

in Routes.php I've set $route['translate_uri_dashes'] = TRUE; . That's mean when i try to enter example.some_path it will be go to example.some-path

in my congfig.php.

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

My .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. BUT example.com/index.php or example.com/index.php/somepath also still available and working too. It will be duplicate content error when visitor access it even I've no-index with robots.txt. So how to maksesure anyting with index.php not accessible? with .htaccess?

2) After set $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). When I try visit example.com/some-path directly without click any link form my site. Yes, example.com/some-path working.

How to makesure index.php can't accessible and automatically redirect to non index.php?

You could try using web.config like I do. I'm not much of a fan of .htaccess Maybe something like:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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