-
开启phpstudy扩展,勾选“rewrite_module”
-
开启phpstudy伪静态支持,打开httpd-conf,修改所有“AllowOverride None”为“AllowOverride All”
修改并保存后,需重启Apache服务器
在网站根目录下添加.htaccess文件,创建时需要使用文本编辑器进行另存为创建,比如Editplus
.htaccess文件代码
RewriteEngine on RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
其中,html后的$符号表示结束,后跟一个空格,$1和$2表示参数
测试:
我的.htaccess代码是
RewriteEngine on RewriteRule article/index-([0-9]{1,}).html$ article/index.html?id=$1
在浏览器地址栏输入 http://localhost/article/index-1.html
会重定向到http://localhost/article/index.html?id=1
在index.html中获取参数的方法为:
function GetRequest() { var url = window.location.href; //获取url中"?"符后的字串 var str=url.split("-")[1].split(".")[0]; return str; } console.log(window.location.href); console.log(GetRequest());
完成。