大网数据广告活动
网站运营与故障问答

How to redirect all requests to HTTPS with SEO friendly URL using Apache?

· 52CE 我爱测

First of all, I would like to leave remove www and redirect all request to HTTPS like this: RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://%…

问题原文

原帖链接 · Adam · CC BY-SA 4.0

First of all, I would like to leave remove www and redirect all request to HTTPS like this:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Furthermore, I have to make this URL friendly:

RewriteRule ^([\w-]+)/?$ index.php?pg=$1 [L,QSA]

I tried to combine these rules:

RewriteRule ^/?([\w]+) https://%1/index.php?pg=$1 [END,QSA]

But it does not work.

For example, I want these URLs to be redirected:

  • http://www.example.com/index.php?p=home
  • http://example.com/index.php?p=home
  • http://www.example.com/home
  • http://example.com/home

to https://example.com/home (so the user only uses/sees this URL)

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

I tried to combine these rules

It looks like you are literally trying to combine these rules into a single rule?! These are two very different things:

  1. Canonicalise the URL (ie. remove the www and redirect to HTTPS). (Although you stated you wanted to "leave www" - your examples and directives suggest the opposite.)
  2. Internally rewrite the request.

As such these should be kept as two separate rules. In which case, the rules you currently have should be working OK as they are? Although you have specified p= in your URLs, whereas your directive uses pg=?

In other words:

RewriteEngine On

# Canonicalise the URL - remove www and redirect to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

# Internally rewrite the "pretty" URL
RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA]

However, you should be consistent: do your URLs end in a slash or not? The above directive currently allows both - which is potentially duplicate content.

  • http://www.example.com/index.php?p=home
  • http://example.com/index.php?p=home

The next (optional) step is to redirect any direct requests to a URL of the form /index.php?p=home (both HTTP and HTTPS, www and non-www) to the "pretty" version /home. This is, again, a separate task and requires a separate rule.

To clarify... you must already be linking to the correct canonical URL in your HTML source. ie. your links already show /home and not /index.php?p=home.

The additional complexity here is that we only want to redirect direct requests and not rewritten requests by the later rewrite.

The following needs to go before the above directives (after the RewriteEngine directive):

# Redirect "old" URLs to new "pretty" URLs
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=([\w-]+)
RewriteRule ^index\.php$ /%1 [QSD,R=302,L]

The QSD flag (Apache 2.4+) is necessary to remove the query string from the redirected URL. (Or can your old URLs contain additional URL parameters that need to be passed?)

The condition that checks against the REDIRECT_STATUS environment variable is necessary to prevent a redirect loop.

Change the 302 (temporary) redirect to 301 (permanent) - assuming that is the intention - only when you are sure this is working OK - to avoid caching issues. You will need to clear your browser cache before testing.

In summary:

RewriteEngine On

# Redirect "old" URLs to new "pretty" URLs
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=([\w-]+)
RewriteRule ^index\.php$ /%1 [QSD,R=302,L]

# Canonicalise the URL - remove www and redirect to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

# Internally rewrite the "pretty" URL
RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-06-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。