Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple rewriterules with .htaccess?

I am trying to build a multilingual website with Drupal.

I like to have the following url format

http://domain/[language]/[node id]

so I added the following rule to .htaccess for testing purpose

RewriteRule ^jpn/[0-9]$ jpn.html

The problem is that the rule is overwritten by the following rule

RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

How do I have multiple rewrite rules?

like image 589
Moon Avatar asked Mar 14 '11 02:03

Moon


People also ask

What is rewrite rule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is $1 rewrite rule?

In your rewrite, the ^ signifies the start of the string, the (. *) says to match anything, and the $ signifies the end of the string. So, basically, it's saying grab everything from the start to the end of the string and assign that value to $1.

What is RewriteCond and RewriteRule?

Rewrite rules also include the ability to use regex matching on the URI before it is executed. In this case, a RewriteCond is not necessary and the condition can be rolled directly into the rule. RewriteRule takes two parameters. A condition based on the request URI on which to execute the Rule.

How do you write rewrite rules in httpd conf?

For instance, to rewrite according to the REMOTE_USER variable from within the per-server context ( httpd. conf file) you must use %{LA-U:REMOTE_USER} - this variable is set by the authorization phases, which come after the URL translation phase (during which mod_rewrite operates).


1 Answers

Your second RewriteRule has the L Flag set, which means that if the rule matches, no further rules will be processed.

If you want your first rule to also stop any further processing, add the L Flag to it as well.

RewriteRule ^jpn/[0-9]$ jpn.html [L]

Also make sure that your second rule is listed last, because it matches everything (.*) and thus, Apache will never see any other rule after it.

Edited: the L Flag URL

like image 148
fx_ Avatar answered Sep 29 '22 21:09

fx_