入门:Apache mod_rewrite方法

Apache’s mod_rewrite module appears to be the one area of Apache administration that generates the most questions. I seem to be a perpetual beginner when it comes to url rewriting, and I continue to dream up impossible wishes on a regular basis.

Apache的mod_rewrite模块似乎是产生最多问题的Apache管理领域之一。 在进行网址重写时,我似乎是一个永不过时的初学者,并且我会继续定期地幻想不可能的愿望。

I also bother my own peers for their best rewrite hacks to build up my arsenal. However, having seen numerous queries on this powerful module, I thought sharing some of my own favorite (and basic) rewriting usage would contribute to those seeking answers.

我也打扰我自己的同僚,以获取他们最好的重写技巧来建立我的武器库。 但是,在看到这个功能强大的模块的大量查询之后,我认为共享一些自己喜欢的(和基本的)重写用法将有助于那些寻求答案的人。

In addition, I am sure we have some hidden rewriting ninjas hanging around the SitePoint community that will add in some missing magic we all could leverage.

另外,我确信我们在SitePoint社区周围悬挂着一些隐藏的重写忍者,这将增加我们所有人都可以利用的一些缺失的魔术。

As a final aside – my knowledge of rewriting has come almost exclusively from reading and re-reading (and re-reading!) the Apache HTTP docs and some serious Googling on the topic over the years. Regular expressions can be both your friend and your enemy! ;>)

最后,我的重写知识几乎完全来自阅读和重新阅读(以及重新阅读!) Apache HTTP文档以及多年来认真讨论该主题的内容。 正则表达式既可以是您的朋友,也可以是您的敌人! ;>)

These are generally added into either your httpd.conf file or into an .htaccess file. For some excellent advice on htaccess if you are using that route – see this.

这些通常添加到您的httpd.conf文件或.htaccess文件中。 有关使用htaccess的一些绝妙建议, 请参阅

Re-directing a sub-domain to a domain:

将子域重定向到域:

RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*).mydomain.com$ RewriteCond %{HTTP_HOST} !^www.mydomain.com$ RewriteRule (.*) http://mydomain.com/%1$1 [P]

RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*).mydomain.com$ RewriteCond %{HTTP_HOST} !^www.mydomain.com$ RewriteRule (.*) http://mydomain.com/%1$1 [P]

Or if you want to force www.mydomain.com — even if visitors use mydomain.com to get to your site:

或者,如果您想强制使用www.mydomain.com,即使访问者使用mydomain.com来访问您的网站,也可以:

RewriteEngine On RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R,L]

RewriteEngine On RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R,L]

Perhaps you have a directory you need to point visitors toward within the DocumentRoot that contains your entry pages (like a beta test site):

也许您有一个目录,需要将访客指向包含您的条目页面的DocumentRoot(例如Beta测试站点):

RewriteEngine On RewriteCond %{HTTP_HOST} ^www.betadomain.com$ RewriteCond %{REQUEST_URI} !^/beta/ RewriteRule ^(.*)$ /beta/$1

RewriteEngine On RewriteCond %{HTTP_HOST} ^www.betadomain.com$ RewriteCond %{REQUEST_URI} !^/beta/ RewriteRule ^(.*)$ /beta/$1

Re-directing users (and search engines) permanently to a new domain from an old domain during a migration (and letting them get to the page they wanted instead of just the new site’s root index page).

在迁移过程中,将用户(和搜索引擎)从旧域永久重定向到新域(并让他们转到所需的页面,而不仅仅是新站点的根索引页面)。

RewriteEngine on RewriteCond %{HTTP_HOST} ^olddomain.com$ RewriteRule ^(.*)$ http://newdomain.com$1 [R=301,L] RewriteCond %{HTTP_HOST} ^www.olddomain.com$ RewriteRule ^(.*)$ http://newdomain.com$1 [R=301,L]

RewriteEngine on RewriteCond %{HTTP_HOST} ^olddomain.com$ RewriteRule ^(.*)$ http://newdomain.com$1 [R=301,L] RewriteCond %{HTTP_HOST} ^www.olddomain.com$ RewriteRule ^(.*)$ http://newdomain.com$1 [R=301,L]

And finally — I see many questions about search engine friendly url’s and dynamic pages. I have used this simple rewrite I learned from a friend – however – this is where some contribution would be great as I see these questions over and over in the forums. By all means add your best rewrite methods for working with dynamic pages. I do not use this last tactic too often and it is quite rudimentary.

最后,我看到许多有关搜索引擎友好的URL和动态页面的问题。 我使用了从朋友那里学到的简单重写-但是-在这个问题上,我会在论坛上一遍又一遍地看到这些问题,因此会有所作为。 务必添加用于处理动态页面的最佳重写方法。 我不经常使用最后一种策略,这很基本。

Allow www.domain.com/news5.php to rewrite to www.domain.com/news.php?id=5

允许www.domain.com/news5.php重写为www.domain.com/news.php?id=5

RewriteEngine on RewriteBase /articles RewriteRule ^news([^\.]+)\.php$ news.php?id=$1

RewriteEngine on RewriteBase /articles RewriteRule ^news([^\.]+)\.php$ news.php?id=$1

翻译自: https://www.sitepoint.com/getting-started-apache-mod_rewrite-methods/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值