[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems mod_rewrite

Posted on 2012-09-18
4
Medium Priority
?
791 Views
Last Modified: 2012-10-22
Hi experts,
we have created a rewrite-rule with mod_rewrite on an apache based webserver. The rule should rewrite every request without "www" to a request with "www".

testpage.com -> www.testpage.com

Our rule works fine, but when a request uses HTTPS without the "www", the request is changed to with "www" but on HTTP. Is there a way to hold the protocol?

Our rules looks like this:
RewriteEngine on

RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteCond %{HTTP:Host} ^(testpage\.com)$ [NC]
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

Best regards,
Marcel
0
Comment
Question by:Systemadministration
  • 2
3 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38413033
RewriteCond %{SERVER_NAME} ^!www\.
RewriteRule .* %{REQUEST_SCHEME}://www.testpage.com/
0
 

Author Comment

by:Systemadministration
ID: 38413078
OK, but this works only for the root.

testpage.com -> www.testpage.com

But the rule must work for subdirs and all other requests:

https://testpage.com/test/test.html -> https://www.testpage.com/test/test.html

But there must be an exemption like this:
RewriteCond %{HTTP:Host} ^(testpage\.com)$ [NC]

Because without this condition, requests with localhost or the ip-adresse also gets rewritten to www.localhost or www.ip .

Best regards,
Marcel
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 1000 total points
ID: 38415160
# add your exception as you need, then
RewriteRule (.*) %{REQUEST_SCHEME}://www.testpage.com/$1[QSA]
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question