Remove 'index.php' from URL with .htaccess

只谈情不闲聊 提交于 2019-12-17 04:01:26

问题


I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
    suPHP_ConfigPath /home/user/php.ini
    <Files php.ini>
        order allow,deny
        deny from all

    </Files>
</IfModule>

This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?


回答1:


This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

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



回答2:


Symfony 2 has an excellent solution:

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]


RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/index.php [L]

This accomplishes the following:

  • RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
  • index.php is removed if present
  • The request is routed to the correct index.php with the full query string from the original request

Note that the line:

RewriteRule ^(.*) - [E=BASE:%1]

is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.




回答3:


I tried this and works fine:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Exceptions

If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:

RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]

If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:

RewriteRule ^(.*)$ index.php/$1 [L]

If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

To:

RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]

And change:

RewriteRule ^(.*)$ /index.php/$1 [L]

To:

RewriteRule ^(.*)$ testing/index.php/$1 [L]

If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:

RewriteRule ^(.*)$ /index.php?/$1 [L]



回答4:


To remove index.php from urls on apache2.4 you can use the following rule :

 RewriteEngine on

 RewriteRule ^index\.php/(.+)$ /$1 [L,R]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]

This will change the uri

/index.php/foobar

to

/foobar

This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.




回答5:


Updated Answer

These days I just use the Wordpress .htaccess as a base: it is relatively simple and works as expected:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

It's also straightforward to force https:// if you desire:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST} [L,R=301]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Original Post from 2012

My initial suggestion was to first do a hard redirect (to strip index.php) and then an internal redirect back to index.php.

The problem is that a greedy internal redirect match (e.g. using (.*)) seems to trigger the hard redirect again, leading to an infinite redirect loop.

I feel that there is a more elegant solution than what I'm about to suggest, but unfortunately I don't have the Apache know-how to think of it.

What I would do is to have a RewriteRule that performs a hard redirect to strip out the index.php and then a set of internal redirect rules to take it from there. For example:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
RewriteRule ^pie$ /index.php?pie [L,QSA]
</IfModule>

This does seem to work correctly without producing any 500 errors. But again, I recommend a pinch of salt.

While that's a far cry from a good solution, I hope it helps.




回答6:


I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc.

I decided to search again today for a solution, one of many times attempted.

This is the one (the last answer shown above) that solves my problem.

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

As an example. this is what it achieves for me.

http://www.domainname.co.uk/index.php/categories/url-name.html

becomes

http://www.domainname.co.uk/categories/url-name.html

Thank you to everyone who contributed to the original question as it lead to the answer and solution above.

Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using.

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future.



来源:https://stackoverflow.com/questions/9608366/remove-index-php-from-url-with-htaccess

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