Apache mod_rewrite Clean URLs

2019-08-28 20:55发布

问题:

I am using CodeIgniter on PHP and it produces the following URLs:

http://my.domain.com/app/index.php?/admin/main/

the /index.php? is redundant so I remove it successfully with the following rewrite rule:

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ ./index.php/$1 [L]

however, I would also like to remove the /app part, but when I do so, I get a 500 error. Is this related to Code Igniter - or how do I rewrite this rule correctly (if possible)? I tried with the following:

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ ./app\/index.php/$1 [L]

Thanks!

回答1:

Sorry but that is a really old way of writing the htaccess for CI if the htaccess is in the main dir of the project just use the universal htaccess code below (this does not need excluded directories and such like the old one i.e.

RewriteCond $1 !^(index\.php|images|robots\.txt)

so it is also better and like I said universal)

*It will not solve your issue with the directory but this is the best way for CI in handling the index.php removal without having to go back each time you add say a docs folder or something.

Awesome htaccess:

# Customized error messages.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php

# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>


回答2:

Try adding rewritebase RewriteBase /Path/to/directory/DirectoryBelowApps/