快轉到主要內容

5 Tips on Creating Redirects in .htaccess

344 字2 分鐘00
作者Y Cheung
目錄

Per-directory rewrites need rewriting within <Directory> sections or .htaccess files, which introduces some complexity. However, if you only have permission to modify files, you must know how to put redirects in .htaccess files with RewriteRule.

1RewriteEngine on
2RewriteRule ^/index\.html$ /index.php [L]

A RewriteRule consists of three arguments separated by spaces.

The arguments are

  • Pattern: which incoming URLs should be affected by the rule, ex ^/index\.html$;
  • Substitution: where should the matching requests be sent, ex /index.php;
  • [flags]: options affecting the rewritten request, ex [L].

Here are 5 tips for creating redirects in .htaccess files.

1. Browser will cache 301 redirects#

Browsers cache 301 redirects permanently unless cache directives in the HTTP response headers clearly state otherwise. The only method to erase these caches is to clear the browser cache, execute a hard reload, or disable caching entirely.

2. Handle special character in RewriteRule#

Special characters like &and ? will be translated to their hexcode counterpart. The [NE] flag stops this from occurring.

The above example redirects /anchor/xyz to /bigpage.html#xyz. If the [NE] is not present, the # will be transformed to its hexcode equivalent, %23 , resulting in a 404 Not Found error situation. Please keep in mind that the # symbol may only be used in a substitution, not in a pattern.

1RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]

3. Handle non-alphanumeric characters in RewriteRule#

In Apache 2.2, a B flag is introduced to escape non-alphanumeric characters before executing the transformation. And you should use non-alphanumeric characters directly in patterns rather than after url encode, ex using coleen's-story instead of coleen%27s-story .

4. Handle multiple lines RewriteRule in one .htaccess#

The most specific should be at the top, and the least specific should be at the bottom. Also, make sure you’re stopping processing with [L] after each redirect rule to prevent Apache from processing subsequent rewrite rules once it finds a match.

5. Handle space in RewriteRule#

Using a \ before the space to escape it instead of %20 , ex:

1RewriteRule ^county/phone\ number$ /front-door [R=301,L]

Testing your htaccess rewrite rules#

Online testing tools recommends:

分享

相關文章

SEO 結構化資料指南

作者 Y Cheung801 字2 分鐘00

結構化資料是一種標準化格式,它提供網頁相關資訊並將網頁內容分類,有助於優化搜索引擎SEO。 ⚠ 即使網頁已正確加上標記,Google 也不能保證結構化資料一定會出現在搜尋結果中。

Wordpress主题添加ogp协议meta标签

作者 Y Cheung459 字1 分鐘00

OGP(The Open Graph protocol)协议是一种新的HTTP头部标记,即Open Graph Protocol,即这种协议可以让网页成为一个“富媒体对象”。由于现 …

修復a2ensite 報錯 Site does not exist

作者 Y Cheung208 字1 分鐘00

給ubuntu主機配置一個子域名的時候,發現用 a2ensite 居然報錯了!明明看了好些教程,比如這個,這個,都是講的差不多的東西,我都完全照著做,怎麼我一運行這個命令就報錯 …

WordPress 301重定向插件 Redirection

作者 Y Cheung1700 字4 分鐘00

朋友博客换了空间换了域名,但是又不想失去苦苦经营多年的旧博客的流量,于是想说那就设置301重定向吧。 首页和某些特定页面重定向到还好办,直接修改.htaccess就好了,就是那差不 …

添加jQuery lazy load plugin插件

作者 Y Cheung133 字1 分鐘00

有时候写日志会添加很多很大的图片,且这些图片所在图床由于我国特殊国情常常loading得非常慢。 为了提高访客的愉悦体验,特添加了这个插件,用来延迟加载图片。

↑