user1240207
user1240207

Reputation: 217

Rewrite Rule Redirection on .htaccess

I have rewrite rule which works on subdomain other than www. I want to force all URLs to start with www and process all the .html with a php:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^catalog\.html$ http://www.domain.com/static.php?staticpage=$1 [L]

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If you hit http://domain.com/catalog.html, it successfully redirects to http:// www.domain.com/catalog.html (without space of course) given that the file does not exist. Or it will just display http:// www.domain.com/catalog.html and renders the actual .html file (without space) if catalog.html exist.

I am trying to achieve http://www.domain.com/catalog.html without displaying the static.php on the browser regardless if the file exists or not. static.php should process every .html page

Thanks in advance!

Upvotes: 0

Views: 261

Answers (2)

ThinkingMonkey
ThinkingMonkey

Reputation: 12727

I am assuming you want to display contents of static.php when a .html file is requested for. without the static.php getting displayed on the browser.

When you use, http:// an external redirection takes place resulting in the address getting displayed on the browser.

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.+)\.html$ static.php?staticpage=$1 [L]

For HTTPS you can follow what @TerryE has suggested. But, I fail to understand what pages are you trying to redirect to https

Upvotes: 0

TerryE
TerryE

Reputation: 10878

The %variables from from the last cond regexp, and you need the condition to succeed so how about:

RewriteCond %{HTTPS}s ^..(s?)
RewriteRule ^         -       [E=PROTO:http%1]

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^            %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 1

Related Questions