How do I remove index.php from the URL?

感情迁移 提交于 2019-12-13 07:08:39

问题


I'm trying to remove the index.php from the url with .htaccess and I have come half way and I can't figure out what is wrong.

I have/using the following settings/system:

  • Codeigniter 2.1.4
  • Ubuntu 12.04
  • $config['base_url'] = 'http://localhost/present';
  • apache2
  • php5

When I'm trying the following urls:

http://localhost/present/test it displays the page. So far so good.

http://localhost/present/index.php/test it also displays the page so I get duplicate pages. And that is my problem.

I'm using the following .htaccess in the same folder as index.php.

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

Can some friendly soul tell me what is wrong? Is it the mod_rewrite or maybe some server/php settings that are wrong?


回答1:


To make fancy url's, you'll need an internal rewrite (which you have) and an external redirect (which you don't have). The difficult part is not making an infinite loop.

If you are using Apache 2.3.9 or up, you can use the END-flag.

#Redirect
RewriteRule ^index\.php/(.*)$ $1 [R,L]

#Internal Rewrite
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php/$0 [END]

If you are using a lower version, you'll need to use a hack-ish workaround. THE_REQUEST will only be set like this on an actual request, not on an rewrite.

#Redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/index\.php [NC]
RewriteRule ^index\.php/(.*)$ $1 [R,L]

#Internal Rewrite
RewriteRule ^(.*)$ index.php/$0 [L]



回答2:


This should work.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !no-redir
RewriteRule ^/?index.php/(.*)$ $1 [R=301,NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule .* index.php/$0?no-redir [L]



回答3:


Try this:

DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

I use this to redirect all requests to index.php



来源:https://stackoverflow.com/questions/18290091/how-do-i-remove-index-php-from-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!