I want to rewrite all urls that start with a predefined language and move the language to a param.
For example:
example.com/de/home to example.com/home?lang=de
example.com/en/home to example.com/home?lang=en
I have this rule in my Apache config file
RewriteRule ^/?(en|de)/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
The problem is it fires multiple times:
example.com/en/en/home to example.com/home?lang=en
example.com/en/de/en/de/home to example.com/home?lang=en
How can I limit the rule to just one occurrence of the language?
Thanks
The syntax is the issue. The ?():
?(en|de)
is a non-capturing group. To capture, remove the ?:
(en|de)
References
PCRE Man Page
RewriteRule Flags
Related
I'm trying to integrate an open source forum in to my WordPress installation, I can figure out the next steps if I can just get a rewrite rule to work, I have the following so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/qa\-theme/(.*) forum-embed/qa-theme/$1 [QSA,L]
RewriteRule ^forum/qa\-content/(.*) forum-embed/qa-content/$1 [QSA,L]
RewriteRule ^forum/([\w]+)$ forum/?url=$1 [QSA,L]
</IfModule>
The first two rules work, but the last one, I've tried all sorts of changes to this regular expression - I want to take whatever comes after forum/ and to put it in to a query string as the url parameter. I'm sure I'm tip-toeing around the expression - what am I missing?
Thanks in advance!
EDIT
It's also not clear how you are avoiding conflicts with the WordPress front-controller? Presumably you are placing these directives at the top of the .htaccess file, before the # BEGIN WordPress section? However, it may be simpler to create another .htaccess file inside the /forum subdirectory instead and this will (by default) override the WordPress directives.
A sound point, yes I was putting it above the # BEGIN WordPress, but I will make a .htaccess in the forum directory.
You say you've "tried all sorts of changes to this regular expression", but this regex certainly won't match your first example. The \w shorthand character class excludes slashes and hyphens.
True, this was a bad example to show where I was up to on my question, but I've also tried:
^forum/(.+)$
^forum/([a-z-A-Z-0-9-/]+)$
/forum/ is presumably a filesystem directory - this itself can't handle the request, it requires further rewriting to an actual file
I don't understand -- the first two rules work, and I can navigate to all pages, including forum/ -- index.php is the default file in the config, why must this rule be an exception?
RewriteRule ^forum/([\w]+)$ forum/?url=$1 [QSA,L]
Example 1: forum/2/test-question => forum/?url=2/test-question
You say you've "tried all sorts of changes to this regular expression", but this regex certainly won't match your first example. The \w shorthand character class excludes slashes and hyphens. If you want to match "whatever comes after forum/", then you could just use (.+) (like your previous examples, except + instead of * to avoid a rewrite loop, ie. to avoid matching /forum/). For example:
RewriteRule ^forum/(.+) forum/?url=$1 [QSA,L]
However, forum/?url=whatever is still not a valid end-point (as #RavinderSingh13 has pointed out in comments). /forum/ is presumably a filesystem directory - this itself can't handle the request, it requires further rewriting to an actual file (perhaps you are expecting mod_dir to issue a subrequest for the DirectoryIndex?). For example, should it be /forum/index.php?url=whatever?
It's also not clear how you are avoiding conflicts with the WordPress front-controller? Presumably you are placing these directives at the top of the .htaccess file, before the # BEGIN WordPress section? However, it may be simpler to create another .htaccess file inside the /forum subdirectory instead and this will (by default) override the WordPress directives.
You should remove the <IfModule> wrapper since it's not required here.
UPDATE:
/forum/ is presumably a filesystem directory - this itself can't handle the request, it requires further rewriting to an actual file
I don't understand -- the first two rules work, and I can navigate to all pages, including forum/ -- index.php is the default file in the config, why must this rule be an exception?
We don't know what requests the first two rules are expected to handle, but I assume they are just rewriting static files?
When you request the directory /forum/ then mod_dir must later issue a subrequest for the DirectoryIndex document. When you rewrite the request to /forum then mod_dir must still perform this additional processing later. In the meantime rewrite processing loops in .htaccess and /forum/ is passed back through the rewrite engine. This may or may not work - it can result in other conflicts - at the very least it is additional/unnecessary processing. You should rewrite directly to the file that handles the request to cut out this additional processing. In the same way the WordPress code block rewrites the request to /index.php, not /.
To clarify, when you request /forum/ only then the above directive is not triggered and mod_dir issues a subrequest for /forum/index.php. There is no url parameter.
Updated directives
However, if rewriting to /forum/index.php, you'll need additional checks to avoid /forum/index.php being caught by the same rule and resulting in a rewrite loop (500 error).
For example, try the following instead:
RewriteRule ^forum/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^forum/(.+) forum/index.php?url=$1 [QSA,L]
The condition that checks against REQUEST_FILENAME may be optional, depending on whether there are any static resources served from this directory tree?
Alternatively, if your URLs do not contain dots then you may get away with a more restrictive regex instead to avoid matching URLs containing dots. For example:
RewriteRule ^forum/([^.]+)$ forum/index.php?url=$1 [QSA,L]
/forum/.htaccess
If moving these directives to the /forum/.htaccess file you would rewrite them as follows (and remove the RewriteBase directive entirely):
RewriteEngine On
RewriteRule ^qa-theme/(.*) /forum-embed/qa-theme/$1 [L]
RewriteRule ^qa-content/(.*) /forum-embed/qa-content/$1 [L]
RewriteRule ^([^.]+)$ index.php?url=$1 [QSA,L]
The QSA flag is not required on the first two directives since the query string is passed through by default. (Although if these are rewriting static resources then you wouldn't expect a query string to be passed anyway?)
No need to backslash-escape the hyphen in the regex, since it carries no special meaning when used outside of a character class. Likewise, the dot carries no special meaning when used inside a character class so does not need to be backslash-escaped in the last rule above.
I'm trying to rewrite URLs such as
/product/16/var1/value1/var2/value2...
to this
index.php?page=product&id=16&var1=value1&var2=value2...
In other words, I would like to have a "main parameter" translated to an id (and I can do this), but I would also like to have, from that point on, couples of "directories" translated recursively to key-value pairs.
Is this possible with Apache mod_rewrite?
In the absence of the [L] flag, any mod_rewrite rule will apply repeatedly to any URI which corresponds to the rule's rewrite conditions and pattern.
Knowing this, we can build a mod_rewrite rule which looks for any URIs with query strings beginning in a certain way and then repeatedly harvests the folder-names of that URI (two at a time) to build the rest of the query string.
See example below:
In the root folder of
http://example.com/
save an .htaccess file with the following mod_rewrite directives:
RewriteEngine On
RewriteRule ^(product)/([0-9]{2})/(.*) http://%{HTTP_HOST}/$3/index.php?page=$1&id=$2
RewriteCond %{QUERY_STRING} ^(page=product&id=[0-9]{2}.*)
RewriteRule ^([^/]+)/([^/]+)/(.*/)?index.php$ http://%{HTTP_HOST}/$3index.php?%1&$1=$2
Using the above:
http://example.com/product/16/var1/value1/var2/value2/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2
and
http://example.com/product/16/var1/value1/var2/value2/var3/value3/var4/value4/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2&var3=value3&var4=value4
I know .htaccess rules are parsed top to bottom but what if my URL matches two rules which one will be used and why?
I have simple rules like
^(.*)$ index.php?pag=cms&title=$1
^store/(.*)$ index.php?pag=store&id=$1
Basically any URL will match the first rule so what happens with other ones?
If the URL matches two rules, it's the first one that rewrites. This is not to say that the second rule doesn't fire. It does but it fails to match because subsequent rules fire on the output of the rule preceding it.
If somehow you don't want the rewrite to fall-through and stop at the first matching rule you can mark the rule as last by using the [L] flag.
^(.*)$ index.php?pag=cms&title=$1 [L]
^store/(.*)$ index.php?pag=store&id=$1 # won't fire now
My .htaccess is as follows:
RewriteEngine On
RewriteRule ^client/([0-9a-zA-Z]+)/(.*)$ licensee/client/$2?id=$1 [L,QSA]
RewriteRule ^licensee/(.*)/$ licensee/$2?id=$1 [L,QSA]
If I visit /client/asdf/ in my browser however, it uses the second rewrite rule instead of the first. If I comment out the second rewrite rule, it uses the first rule as expected.
I can't figure out how the second rule could possibly match /client/asdf/ - have I overlooked something?
When you visit /client/asdf/, it matches the first RewriteRule, so a new request becomes "licensee/client/?id=asdf" which matches your second rule.
I'm trying to remove query strings from my calendar, but my mod_rewrite is not appending the query string.
The website is http://cacrochester.com/Calendar
and if you click the link to go to a different month, the query string is usually http://cacrochester.com/Calendar?currentmonth=2010-11
With my rule below, it just doesn't append the query string so when you click the next month link, it just stays on the month October. What's wrong with my rule?
Here is my rule
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^.*$ http://cacrochester.com/Calendar? [NC,R=301,L]
EDIT:
What i want is to take a url like http://cacrochester.com/Calendar?currentmonth=2010-11 and turn it into something like http://cacrochester.com/Calendar/2010-11
You probably need your app to output relative urls like "/Calendar/2010-11". That's a simple code change.
Then in Apache you'd want to rewrite those urls, using:
RewriteRule ^/Calendar/([0-9]+-[0-9]{2})$ /Calendar.php?currentmonth=$1 [NC,QSA,L]
(You don't want a RewriteCond for this rule.)
Forcing a redirect with R=301 will only expose the internal url scheme. I don't think that's what you want.
To maintain query strings when rewriting, use the QSA (query string append) flag.
[NC,R=301,QSA,L]