使用https重写SEO友好URL

时间:2014-08-13 09:29:11

标签: php .htaccess https

我最近设置了一个拥有SSL证书的域名,我想通过https而不是http来访问整个网站。

我目前正在使用RewriteEngine和一个名为page.php的php脚本来处理URL参数,以便将正确的页面内容包含在SEO友好的URL中,如下所示。

www.example.com/page.php?venue=someplace&artist=singer

会变成:

www.example.com/someplace/singer

我的.htaccess文件包含以下内容

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ page.php?venue=$1&artist=$2&%{QUERY_STRING} [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ page.php?venue=$1&artist=$2&%{QUERY_STRING} [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?venue=$1&%{QUERY_STRING} [L]
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?venue=$1&%{QUERY_STRING} [L]

通过https访问网站时,重写似乎无法正常工作。用户将被定向到我的404错误页面。在.htaccess文件中在https://www.example.com/之前添加page.php没有达到预期的效果。而是将用户转发到https://www.example.com/page.php?venue=test&artist=test。虽然这不是我要求的SEO友好URL,但是这个URL正确加载页面内容,所以只能假设问题必须在我的.htaccess文件中。

我搜索了解决方案并找到了以下我尝试过的内容,包括我的.htaccess,但没有取得任何成功。

RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [Rs,L]

是否有人有解决方案可以输入我的.htaccess文件以使我能够通过https访问该网站?

1 个答案:

答案 0 :(得分:0)

保持你的.htaccess像这样:

RewriteEngine On
RewriteBase /

# enforce https and www
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=302]

# skip further rules for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal rewrite rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ page.php?venue=$1&artist=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ page.php?venue=$1 [L,QSA]