Link to home
Start Free TrialLog in
Avatar of Systemadministration
Systemadministration

asked on

Problems mod_rewrite

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
Avatar of ahoffmann
ahoffmann
Flag of Germany image

RewriteCond %{SERVER_NAME} ^!www\.
RewriteRule .* %{REQUEST_SCHEME}://www.testpage.com/
Avatar of Systemadministration
Systemadministration

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial