简体   繁体   中英

Removing index.php from URLS with .htaccess

I have an index.php controller what all URLs that aren't existing files redirect to. The .htaccess rules I currently have look like this:

RewriteEngine On
DirectorySlash Off

# Remove Trailing Slashes
RedirectMatch 302 ^(.*)/$ $1

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302]

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

What I want to do is use a 301 redirect if index.php is included in the URL. BUT! I can't use RewriteBase or queries that start with a slash because the webapp I'm developing could be in a subfolder. So basically, I only want to rewrite for the current directory:

example.com/foo/index.php/bar/whatever should redirect to example.com/foo/bar/whatever example.com/foo/index/bar/whatever should also redirect to example.com/foo/bar/whatever

localhost/place/foo/index.php/bar/whatever should redirect to localhost/place/foo/bar/whatever localhost/place/foo/index/bar/whatever should also redirect to localhost/place/foo/bar/whatever

How can I accomplish this with .htaccess?

UPDATED CODE: Everything here works EXCEPT: index.php is not removed from anywhere in the URL.

Here's the code:

<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]

# Reroute to index.php
#RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]

</IfModule>

Not a good idea to mix mod_rewrite rules with mod_alias ones. Try this updated .htaccess :

<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php[\s?/] [NC]
RewriteRule ^(.*?)index\.php(/.*)?/?$ /$1$2 [L,R=301,NC,NE]

# Reroute to index.php
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cURL=/$1 [L,QSA]
</IfModule>

Make sure you have mod rewrite on on your server try

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

You can use like this

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

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