dongshi6844 2013-05-16 16:36
浏览 49
已采纳

将参数从Mod_rewrite传递到PHP,前端只包含HTML和JS

I have a web application, the front end is HTML & JavaScript/jQuery and The back is PHP.

It was using ugly URL's like so, host/app/Page.php?userid=1.... Ugly! So with a little MOD_REWRITE it will now serve the urls as url/page/1/

RewriteRule    ^page/([0-9]+)/?$    host/app/page.php?userid=$1    [NC,L]  

Next it originally did simple jQuery GET request to the PHP files to get the dynamic data.

var params = A JS function to get all paramters after the ? in URL
var userid = params.id;

$.getJSON('host/app/page_code.php',{'userid':userid}, function(data) {
    var blah = data.blah;
});

Now after using MOD_REWRITE I now have no nice way of getting the front end to the parameters, only in a page/1 format. Looping though and getting each parameter between the slashes just doesn't feel like the right way to do it.

So I hit Google and it seems that sending the parameters from MOD_REWRITE seems like the way to do it. So my questions are, how is this possible, how is it done?

How do I get the parameters from the URL with MOD_REWRITE, send those to the PHP back-end, then return a response to the front end with JSON?

edit: The problem I have is I need to pass Parameters from JS to a PHP back end then get a response. I originally got these parameters from the URL with JS, then passed them into the GET request, I would then get a response from the PHP depending on the Parameter sent. However now I have used mod_rewrite on the URL I can not see the parameters on the front end to catch in the traditional ?a=b&c=d format. it now looks like /b/d To get these in JS I can only do this by looping through and getting each parameter between the slashes. Doesn't seem right to do it like this. I need to actually some how get the parameters to the PHP code in a separate page from the front end. Then pass it back to the front end with the response from the PHP code so I have lets say the users id. I don't want any php in the front end file as to keep it all separated. There must be a way to get the parameters from the url to the PHP code in a different file, then send back a response to the front end HTML&JS with the usersid. I'm new to this whole MOD_REWRITE for clean urls, and it's confusing the hell out of me!

  • 写回答

2条回答 默认 最新

  • dqgg25493 2013-05-16 17:24
    关注

    You could write a reverse router with javascript in which given a URL, it returns the parameters.

    Example (really bad one, just to give you an idea) on jsfiddle

    Drawbacks: you will have to copy the routings from .htacces file to Javascript. Everytime something changes in that file, you will have to change in Javascript too.

    Edit:

    Another approaches

    • Do all the routing processing in PHP, not even using .htaccess and then retrieve data for routing in JSON format. You'd access this from an Ajax request

    Example:

    jQuery.ajax({
        url: 'reverse_routing.php', 
        type: 'json', 
        data: {
            url: window.location.pathname 
        }, 
        function(params) {  
           // Do your stuff here...
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器