如何使用mod_write从网址中删除index.php,同时仍然允许php查看其中带有index.php的路径? [英] How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?
问题描述
在过去的三天里,我一直在使用Apache的mod_rewrite尝试将其从URL中删除index.php,而php仍需要在路径中查看它.
For the past 3 days I have been playing around with Apache's mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.
Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2
While the user needs to see this
http://example.com/Page/Param1/Param2
我现在所拥有的是htaccess文件中的以下内容
What I have right now is the following in an htaccess file
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]
这是从另一页上摘下来的,很接近我的需要.但是,这似乎切断了http://example.com/
部分之后的所有内容.如何获得mod_rewrite向用户显示一件事,让php看到其他东西?
Which was taken from another page, and is close to what I need. However this seems to be cutting off everything after the http://example.com/
part. How can I get mod_rewrite to show the user one thing and have php see something else?
推荐答案
这是您可以在.htaccess(在DOCUMENT_ROOT下)中使用的修改后的代码,用于从URI中删除index.php:
This is the modified 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)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
在对它的运行感到满意之后,将R = 302更改为R = 301.
Change R=302 to R=301 once you're satisfied that it is working fine for you.
这篇关于如何使用mod_write从网址中删除index.php,同时仍然允许php查看其中带有index.php的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!