6

When moving all pages from old domain to new domain, i noticed some people added a ^and other not in rewriterule

What is the different between

RewriteRule ^(.*)$ http://mynewdomain.com/$1 [R=301,L]

and

RewriteRule (.*)$ http://mynewdomain.com/$1 [R=301,L]

thank you.

Nick321
  • 151
  • 1
  • 2
  • 6

2 Answers2

7

They are both wrong. There is no need to match anything in a backreferennce because Apache has a built in variable for the current URL: %{REQUEST_URI}.

RewriteRule .? http://www.newdomain.com%{REQUEST_URI} [L,R=301]

As for your question, the meaning of ^ is 'matches at the start of the URLand$` is 'matches at the end of the URL'. This is probably easiest by example.

^welcome/ matches the url /welcome/a/b/c, /welcome/b/c/d, etc, anything that starts with /welcome

welcome$ matches /a/b/welcome, /a/something/welcome, etc, anything that ends with a 'welcome'

The first RewriteRules you present in your question is 'An URL that has a start and an end and something inbetween', while the second one is 'a URL that has some text and then ends'. Both expressions are very generic and match anything you throw at it.

rpkamp
  • 811
  • 4
  • 14
  • great line. It works great to redirect my old domain to a new domain. Thks. – Nick321 May 13 '13 at 21:50
  • can i use RewriteRule .? http://www.newdomain.com%{REQUEST_URI} [L,R=301] to redirect all pages from old domain to new domain? – Nick321 May 15 '13 at 15:51
  • Together with a `RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [NC]`, yes, you can. – rpkamp May 15 '13 at 17:59
2

It is always better to use start and end anchors (^ and $) wherever possible like ^(.*)$ but in the 2 cases you listed both rules will behave similar.

anubhava
  • 761,203
  • 64
  • 569
  • 643