htaccess redirect specific pages in domain to non-www

I'd appreciate htaccess code to redirect only specific pages in my domain to non-www, for example, page1.htm and page2.htm   Thank you.
LVL 1
ddantesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve BinkCommented:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule /?(page[12]\.htm) http://www.domain.com/$1 [NC,L]

Open in new window


That handles the specific use cases of page1.htm and page2.htm.  If you have a long list of URLs, you'll need to either dupe these rules for them or construct a regex to handle the entire list.

You could also expand this by matching for "/?(.*)" in the rule, then matching against %{REQUEST_URI} in a new RewriteCond directive.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddantesAuthor Commented:
Thank you.  Just to be clear, the following code will redirect www.mauitradewinds.com/view.htm to mauitradewinds.com/view.htm, correct?

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule /?(view\.htm) http://www.domain.com/$1 [NC,L]
0
Steve BinkCommented:
You need to replace "domain.com" with "mauitradewinds.com", but yes, that is correct.

It should be redundant, given the destination URL, but you could also add the [R] flag to indicate an external redirect:
RewriteRule ..yaddayadda..  [NC,L,R]

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

ddantesAuthor Commented:
Thank you.  I added the code which you supplied to my .htaccess, but then the view.htm page would not load in my browser.   Also, I think there was a misunderstanding about the intention.  I think your code would have added (not removed) www from the ultimate URL.

In any case, I found some code which produces the desired result...

# redirect view.htm to non-www if it initially has a www prefix
RewriteCond %{REQUEST_URI} ^/view.htm$
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

I appreciate your input.  May I delete this question, since I have found a solution?
0
Steve BinkCommented:
Yes, the "domain.com" in the RewriteRule should have been without the "www".  While I understood the intention of your question, my fingers remembered it is usually the other way around.

The solution you have found is the same as mine, if less efficient.  The match again %{REQUEST_URI} is unnecessary since that is what the RewriteRule matches originally (the ^(.*)$ regex).  The match against %{HTTP_HOST} is also the same, with the slight difference of using a backreference instead of a static reference for the rule's destination.  Otherwise, the solutions are exactly the same.

When you say "would not load in my browser", what result did you see?
0
ddantesAuthor Commented:
Thank you for your explanation.   When I tested the code which you supplied, I saw "Internet Explorer cannot display this page."   However, since you informed me that the code will perform the same function as the one I found, but more efficiently, I edited yours to the following, and it works:

RewriteCond %{HTTP_HOST} www\. [NC]
RewriteRule /?(view\.htm) http://mauitradewinds.com/$1 [NC,L,R]

Thank you for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.