Linux 拨号vps windows公众号手机端

实用的Apache Rewrite重写规则有哪些

lewis 6年前 (2019-01-27) 阅读数 11 #程序编程
文章标签 apacherewrite

以下是一些常用的Apache Rewrite重写规则:

  1. 将www重定向到非www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
  1. 将非https重定向到https:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  1. 隐藏URL中的文件扩展名:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
  1. 重写URL路径:
RewriteEngine On
RewriteRule ^oldpath/([^/]+)/([^/]+)$ newpath/$1/$2 [L]
  1. 重写URL查询字符串:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^product\.php$ product/%1 [R=301,L]

这些只是一些常见的Rewrite规则示例,您可以根据您的需求进行自定义。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门