Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between RewriteRule ^(.*)$ & RewriteRule (.*)$

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.

like image 340
Nick321 Avatar asked May 13 '13 20:05

Nick321


People also ask

What is RewriteCond and RewriteRule?

There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.

What is Apache RewriteRule?

RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.

What is RewriteRule * F?

F|forbidden The following rule will forbid .exe files from being downloaded from your server. RewriteRule "\.exe" "-" [F] This example uses the "-" syntax for the rewrite target, which means that the requested URI is not modified. There's no reason to rewrite to another URI, if you're going to forbid the request.

What does IfModule mod_rewrite C mean?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.


1 Answers

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.

like image 98
rpkamp Avatar answered Sep 30 '22 15:09

rpkamp