PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 21, 2022

[FIXED] How to create RewriteRule for the specific match https and www together?

 September 21, 2022     apache, ssl, url-rewriting, virtualhost     No comments   

Issue

I have managed to create working redirects for www and for non-www (plain http) and https, but when using them together it doesn't work anymore. I have four domains and want each one to redirect to a base-domain and then add url parameters.

Right now the current result when trying the different variations:

www.example.fi -> redirects me to https://example.se/?lang=fi. OK
http://example.fi -> redirects me to https://example.se/?lang=fi OK
https://example.fi -> redirects me to https://example.se/?lang=fi OK
https://www.example.fi -> redirects me to https://example.se without the lang parameters.  WRONG. It should take me to the same place as the other.

This is my http virtual host:

<VirtualHost *:80>
ServerName www.example.fi
ServerAlias example.fi
DocumentRoot /var/www/html/dir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Redirect permanent / https://example.se/?lang=fi
</VirtualHost>

And this is my https:

<VirtualHost *:443>
ServerName www.example.fi
ServerAlias example.fi
DocumentRoot /var/www/html/dir
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /home/user/example.fi.pem
SSLCertificateKeyFile /home/user/example.fi.key
SSLCACertificateFile /home/user/intermediate.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.fi$ [NC]
RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]

As you can see, i want the different language domains point to the SE-domain with the language parameter.

How can i modify my code so that it matches with all possible variations of https, http and www together?


Solution

in your code

  RewriteCond %{HTTP_HOST} ^example\.fi$ [NC]
  RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]

the Rewrite match only htt(s)://exemple.fi URL

Without ^ it should work

  RewriteCond %{HTTP_HOST} example\.fi$ [NC]
  RewriteRule ^ https://example.se%{REQUEST_URI}/\?lang=fi [R=301,L]


Answered By - R. Martin
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing