简体   繁体   English

使用.htaccess和mod_rewrite改善此网址

[英]Improving this url with .htaccess and mod_rewrite

I have a URL I want to shorten with a mod_rewrite. 我有一个要使用mod_rewrite缩短的URL。

In its ugliest form it looks like: 以最丑陋的形式看起来像:

/img.php?i=15&a=92

Ideally it would look like: 理想的情况是:

/img/15/92

The problem is sometimes it might just be: 问题是有时可能只是:

/img.php?i=15

In which case the person will enter: 在这种情况下,此人将输入:

/img/15

So I'm thinking I need a mod_rewrite like this: 所以我想我需要这样的mod_rewrite:

RewriteRule ^/img/(.*)/(.*)$ /img.php?i=$1&a=$2

Which I imagine will work only when both variables are in the URL and not just the shortened version. 我想只有两个变量都在URL中,而不仅仅是缩短版本时,它才能工作。

How do I make a single rewrite that works for both, or how do I make 2 without cancelling the other out? 如何进行对两者都适用的单个重写,或者如何在不取消另一个的情况下进行2次重写?

Try this rule: 尝试以下规则:

RewriteRule ^img/([0-9]+)(/([0-9]+))?$ img.php?i=$1&a=$3

When using mod_rewrite in a .htaccess file, you need to remove the per-directory path prefix from the pattern (in this case the leading / ). 在.htaccess文件中使用mod_rewrite时,您需要从模式中删除每个目录路径的前缀(在本例中为/ )。 Because mod_rewrite does that too and puts back it after the rewrite process. 因为mod_rewrite也会这样做,并在重写过程之后将其放回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM