redirect example.com/page to www.example.com/page

I have a problem with my htaccess redirects. when I type in example.com/page,it is redirected to example.com instead of example.com/page .How to fix this? Here is the code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^page$ "https\:\/\/www\.example\.com\/page" [R=301,L]

Upvotes: 0

Views: 42

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133610

With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^page/?$ https://www.example.com/page [R=301,L]

Fixes with OP's attempted code:

  • We need not to escape . on right side while url rewriting, that is needed when we are trying to match it as a regex in left side of RewriteRule/RewriteCond part.
  • Then we need NOT use " to cover url part in right side(which you used in your last line of shown htaccess file), its not correct syntax.

NOTE: Also in case your page(shown as per samples) is a non existing directory or file then we may need to write further rules to avoid 404 error or so.

Upvotes: 1

Related Questions