如何编写.htaccess文件以将斜杠后的所有内容用作参数? [英] How to write .htaccess file to get everything after slash as parameter?

查看:81
本文介绍了如何编写.htaccess文件以将斜杠后的所有内容用作参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址,即"www.mysite.com".我想通过url通过以下方式发送参数:

I have a URL i.e "www.mysite.com". I want to send parameters via url in following ways:

www.mysite.com/count
www.mysite.com/search_caption?query=huha
www.mysite.com/page=1
www.mysite.com/search_caption?query=huha&page=1

在每种情况下,我都希望使用以下参数加载index.php页面:

In each of these cases I want to load index.php page with parameters as follows for each case:

var_dump($_REQUEST) results into [count]
var_dump($_REQUEST) results into [query="huha"]
var_dump($_REQUEST) results into [page=1]
var_dump($_REQUEST) results into [query="huha",page=1]

如何编写.htaccess文件来实现这一目标?

How do I write .htaccess file to achieve this?

我正在使用此代码,但它仅捕获?"之后的参数.而不是第一个斜杠之后的所有内容

I am using this code but it is capturing only params after "?" and not everything after first slash

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+)/?$ index.php?{REQUEST_FILENAME}=$1 [L,QSA]
RewriteRule .* /index.php [L]

推荐答案

类似的事情应该会逐渐结束,尽管您确实应该考虑那些奇怪的URL模式,而不是在事后尝试通过重写来解决它们.

Something like that should get close, though you really should think about those strange URL patterns instead of trying to fix them afterwards with rewriting...

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,QSA]

RewriteRule ^count index.php?count=1 [L]

RewriteRule ^page/(.*)$ index.php?page=1 [L]

RewriteRule ^ index.php [L,QSA]

一些注意事项:

  • 前三个RewriteRules是必要的例外,因为给定的请求不遵循理智而通用的​​模式.它们看起来有些混乱.
  • 这当然不是没有问题,我没有测试它,只是打了下来.
  • 这假定要像评论中所讨论的那样请求页面"示例.
  • index.php实际上必须作为文件存在,否则将导致重写循环
  • the first three RewriteRules are exceptions necessary because your given requests do not follow a sane and common pattern. They appear somewhat chaotically chosen.
  • this certainly is not free of issues, I did not test it, only typed it down.
  • this assumes the "page" example to be requested like as discussed in the comments.
  • index.php actually has to exist as a file, otherwise this will result in a rewrite loop

鉴于所有这些重写都应该发生:

Given all that these rewritings should happen:

www.mysite.com/count => index.php?count=1
www.mysite.com/search_caption?query=huha => index.php?query=huha
www.mysite.com/page/1 => index.php?page=1
www.mysite.com/search_caption?query=huha&page=1 => index.php?query=huha&page=1

还请注意,以上规则是为.htaccess样式文件编写的.要用作常规规则,因此在http服务器主机配置内部,它们的编写方式必须稍有不同.如果确实需要,则仅应使用.htaccess样式文件,因此,如果您无权访问配置文件.如果可能,您应该始终避免使用这些文件.众所周知,它们容易出错,难以设置和调试,并且确实会降低服务器的速度.因此,如果您有权访问http服务器配置,则可以在其中定义此类规则.

Also note that the rules above are written for .htaccess style files. To be used as normal rules, so inside the http servers host configuration, they would have to be written slightly different. You should only use .htaccess style files if you really, really have to, so if you have no access to the configuration files. You should always try to avoid those files if somehow possible. They are notoriously error prone, hard to setup and debug and really slow the server down. So if you have access to the http server configuration, then defines such rules in there instead.

这篇关于如何编写.htaccess文件以将斜杠后的所有内容用作参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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