【php踩坑记】利用.htaccess对URL重写。

有个需求:

输入xxx.com/123时,

让服务器转化为xxx.com/index.php?id=123

【最初尝试】

在v目录下新建文件:(格式utf-8)


【index.php】
<?php echo "id:".$_GET['id']; ?>
【.htaccess】
<ifmodule mod_rewrite.c> 
RewriteEngine on 

RewriteRule ^(.+)$ /v/index.php?id=$1 [L] 

【注释:注意空格后的路径,是从根目录算起的相对路径。我最初没有加/v/,结果一直跳转到了根目录的index.php!】

</ifmodule> 
=============================================
好了,我们地址栏打开:【xxx.com/v/123】

坑出现了!!!

这是因为根目录下index.php跟/v/目录下的index.php【重名】!
我们给/v/index.php改下名,.htaccess重新配置下:
=============================================

【正确配置】

【myTest.php】
<?php echo "id:".$_GET['id']; ?>
【.htaccess】
<ifmodule mod_rewrite.c> 
RewriteEngine on 
RewriteRule ^(.+)$ /v/myTest.php?id=$1 [L] 
</ifmodule> 
=============================================
重新输入地址栏:【xxx.com/v/123】
成功!


猜你喜欢

转载自blog.csdn.net/chanlingmai5374/article/details/80499449