如何重写网址以删除问号 [英] How to rewrite a url to remove the question mark

查看:108
本文介绍了如何重写网址以删除问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个更改页面内容的查询字符串,具体取决于用户在网址中的问号后输入的内容。

I have a query string which changes the content of the page, depending on what the user enters after the question mark in the url.

http://mysite.subdomain.com/?3

会加载分配给的内容ID 3

Would load content assigned to the ID 3

http://mysite.subdomain.com/?34

加载分配给34的内容。

Loads content assigned to 34.

我需要输入我的.htaccess文件这样,当用户前往

What do I need to enter into my .htaccess file so that when a user goes to

http://mysite.subdomain.com/1234

它加载了用户输入时将加载的内容

it loads content which would be loaded if the user entered

http://mysite.subdomain.com/?1234

基本上我想要清理网址并删除问号。谢谢。 (我意识到这是一个基本问题)

Essentially I want to clean up the url and remove the question mark. Thanks. (I realise this is a basic question)

推荐答案

无htaccess



由于您的javascript已经处理了URL部分,因此您不需要htaccess。 javascript采用查询字符串,例如?OdfgzaBv2qY ,删除问号并将剩余的 OdfgzaBv2qY 作为ID添加到iFrame中。

None htaccess

Since your javascript already processes the URL part, you don't need htaccess. The javascript takes the querystring, for instance ?OdfgzaBv2qY, removes the questionmark and puts the leftover OdfgzaBv2qY as ID in the iFrame.

由于javascript使用 window.location.search ,脚本需要在问号后设置变量URL。所以www.mysite.com/?3会有效,但是当你访问www.mysite.com/3时它不会,因为 window.location.search 保持空白。

Since the javascript is using window.location.search the script expects variables to be set after the questionmark in the URL. So www.mysite.com/?3 will work but when you visit www.mysite.com/3 it won't, because the window.location.search stays empty.

htaccess无法解决的原因是因为htaccess正在处理服务器端,javascript正在处理客户端。即使htaccess工作正常,你被重定向到 index.html?3 ,javascript仍会看到www.mysite.com/3网址,而不是最终请求。所以基本上:服务器理解变量在后台设置为?3,但javascript看到用户看到的内容:没有问号的友好URL。

The reason why htaccess is no solution is because htaccess is being processed serverside, the javascript is being processed clientside. Even IF the htaccess is working, and you are being redirected to index.html?3 the javascript still sees the www.mysite.com/3 URL and not the final request. So basically: the server understands that the variable is set to ?3 in the background, but javascript sees what a user sees: a friendly URL without a questionmark.

<script type="text/javascript"> 
  //GET DATA FROM URL ...http://www.mysite.com/YOUTUBE_ID 
  var DATA = window.location.pathname.replace("/", ""); 

  window.onload = function() { 
    var YouTube = document.getElementById('YouTube'); 
    YouTube.src = "http://www.youtube.com/embed/" + DATA; 
  }; 
</script>

window.location.pathname应该带 / 3 并替换 / 。而window.location.search采用?3 部分并替换

window.location.pathname SHOULD take /3 and replace the /. Whereas window.location.search takes the ?3 part and replace the ?

基本的.htaccess文件将是:

A basic .htaccess file for this would be:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteRule ^([0-9]+)$ index.php?id=$1 [L]
</IfModule>

Ofcourse index.php?id = 1应替换为您用于执行请求的任何页面。

Ofcourse index.php?id=1 should be replaced with whatever page your are using to execute the request.

一个简短的解释:


  • 规则从^开始然后在基本URL后面查找数值,以防您有 http://www.domain.com/42 规则在最后一次开始之后/

  • $定义了规则正则表达式的结束。

  • index.php? id = $ 1是必须加载的URL(其中$ 1是参数1:([0-9] +)

  • [L]是一个标志Last,表示htaccess不应该寻找适用的其他规则

  • the rule starts with the ^ and then looks up for a numeric value behind the base URL, so in case you have http://www.domain.com/42 the rule starts right after the last /
  • The $ defines the end of the regex of the rule.
  • index.php?id=$1 is the URL that has to be loaded (where $1 is parameter 1: ([0-9]+))
  • the [L] is a flag "Last", which indicates that the htaccess should not look for other rules that apply

注意这不适用于 http://www.domain.com/49/ ..因为斜线最后是不匹配的。这可以通过以下方式完成:

Note that this does not work for http://www.domain.com/49/ .. since the slash at the end is no match. This can be done by:

RewriteRule ^([0-9]+)/$ $1 [L,R=301]
RewriteRule ^([0-9]+)$ index.php?id=$1 [L]

首先查看是否有斜杠,如果是,则重定向到slashlessurl(R = 301:Redirect 301 =永久重定向),然后匹配第二个规则并执行请求。

this first looks if there's a slash, if so it redirects to the "slashless" url (R=301: Redirect 301 = permanent redirect), and then it matches the 2nd rule and executes the request.

这篇关于如何重写网址以删除问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆