apache rewriterules - mod-rewrite

How can I match a url like that:
1/2/3/
it means:
1 = http:// www.mysite.com
2 = may be mypage or my-page
3 = may be myparam1 or myparam-1 or my-param-1
--> 1 should be pre-defined
--> 2 and 3 may contain only letters or alphanumeric or alphanumeric plus one hyphen or two (-)
I need a reqular expression to use with htaccess to rewrite a url to another url with some $_GET params.
Thanks in advance.

Try this rule:
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} =www.example.com
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ foo?1=$1&2=$2

Related

mod_rewrite, help me forward a page

I'm sorry but I don't really completely understand how mod_rewrite works but I'd like to basically change the url:
/index.php?category=value1&video=value2
to be accessed via /value1/value2
could anybody tell me how to do this? thanks^^
Try this one here:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ index.php?category=$1&video=$2 [L]
The first line enabled the usage from the mod_rewrite.
The second line is a condition which checks if there is a file with that name. If not continue with the next line.
The third one is a regular expression. The ^ markes the beginning and the $ the end of it. The /? means that at the beginning should be a optional / (this depends on the server configuration). The (.*) meanes a range of chars which from 0 until n. The brackets meanes that there is a group which can be called as $n here as $1 and $2.
Please note that AllowOverride All must be enabled in the server configuration.

RewriteRule to accept 3 parameters but 2nd & 3rd param are optional

mydomain.com/MyFolder/parameter-1
I have this htaccess RewriteRule -
RewriteRule ^([a-z0-9\-]+)/?$ index.php?c=$1 [NC,L]
The htaccess file is inside [MyFolder] and this only accept a single parameter.
How can do a RewriteRule to accept 3 parameters but the 2nd and 3rd parameters are optional so that the following 4 URLs are ALL possible
mydomain.com/MyFolder/param-1/param-2/param-3
mydomain.com/MyFolder/param-1
mydomain.com/MyFolder/param-1/param-2
mydomain.com/MyFolder/param-1/param-3
Edit : Ill make it that PARAM3 always starts with the word [pop]
Thanks
RewriteRule ^([a-z0-9\-]+)/?([a-z0-9\-]+)?/?([a-z0-9\-]+)?/?$ index.php?c=$1&d=$2&e=$3 [NC,L]
This would work for:
mydomain.com/MyFolder/param-1/param-2/param-3
mydomain.com/MyFolder/param-1
mydomain.com/MyFolder/param-1/param-2
with $_GET[d] and $_GET[e] having as value '' in certain cases.
Having:
mydomain.com/MyFolder/param-1/param-2
mydomain.com/MyFolder/param-1/param-3
Seems impossible to me as is, how can you know if the second is param 2 or param 3 ?

How can i set optional parameter in the Mod Rewrite-Syntax?

I'm german and i'm sorry about my damn sucking englisch.
RewriteRule ^admin/(.*)$ index.php?admin=$1
here is an example.
so the result i want is for example the id within, but as an optional parameter:
RewriteRule ^admin/(.*)/id-(.*)$ index.php?admin=$1&id=$2
Here the id don't must set thats what i want :)
The Link example (i want that here is the same result):
www.exmple.com/admin/edit-user
www.exmple.com/admin/edit-user/id-1
the problem is: the rule take anytime the first way with only this:
RewriteRule ^admin/(.*)$ index.php?admin=$1
can anybody help me ? :)
loving greetings m0.
Try changing the order of your rules and don't forget the [L] flag at the end :
RewriteRule ^admin/(.*)/id-(.*)$ index.php?admin=$1&id=$2 [L]
RewriteRule ^admin/(.*)$ index.php?admin=$1 [L]
Try an optional capture group:
RewriteRule ^admin/([^/]*)(/id-(.*))?$ index.php?admin=$1&id=$3
If there's no id specified, then $3 will be blank and you'll just get a blank id rewritten:
index.php?admin=edit_user&id=
If it is specified, then you'll get the id too:
index.php?admin=edit_user&id=1
The expression .* is too general as it matches an arbitrary length of arbitrary characters. A better expression would be [^/]+ that matches only one ore more arbitrary characters except / (path segments separator).
So try these rules:
RewriteRule ^admin/([^/]+)$ index.php?admin=$1
RewriteRule ^admin/([^/]+)/id-([^/]+)$ index.php?admin=$1&id=$2
For the second rule you could also replace the second [^/]+ with [1-9][0-9]* that would match only integers greater than 0.
RewriteRule ^admin/([^/]+)/id-([1-9][0-9]*)$ index.php?admin=$1&id=$2
try this:
RewriteRule /admin/([^/]*)/?(id-(.*))?$ index.php?admin=$1&id=$3

Isapi Rewrite - access query string pattern matches in RewriteRule

I'm using Isapi Rewrite 3 (mod rewrite clone for IIS) and trying to rewrite URLs based on the query string - and then pass on part of that query string in the rewrite.
So if I enter a URL like this: /test/home.cfm?page=default&arbitraryExtraArg=123
I want that to be rewritten as: /test/index.cfm?page=home&arbitraryExtraArg=123
I have the following condition/rule:
RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home.cfm$ /test/index.cfm?page=home%1 [I,R=301]
But the extra query string variables are never passed. %1 seems to be blank.
This is how to reference a pattern match from the RewriteCond, right?
What am I missing here?
Thanks!
EDIT: It looks to me like the RewriteCond is being totally ignored. Here is what my logfile looks like:
[snip] (2) init rewrite engine with requested uri /test/home.cfm?page=default
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/home.cfm'
[snip] (1) escaping /test/index.cfm?page=home
[snip] (2) explicitly forcing redirect with http://www.devsite.com/test/index.cfm?page=home
[snip] (2) internal redirect with /test/home.cfm?page=default [INTERNAL REDIRECT]
[snip] (2) init rewrite engine with requested uri /test/index.cfm?page=home
[snip] (1) Htaccess process request C:\Inetpub\wwwroot\dev\IsapiRewrite\httpd.conf
[snip] (3) applying pattern '^/test/home\.cfm$' to uri '/test/index.cfm'
Should there be mention of the RewriteCond pattern check in there?
But the extra query string variables are never passed. %1 seems to be blank.
%1 is blank because according to the log the request you make is /test/home.cfm?page=default - without second parameter.
The absense of RewriteCond processing in the log may be due to low RewriteLogLevel.
And the config should be:
RewriteCond %{QUERY_STRING} ^page=default(.+)$ [NC]
RewriteRule ^/test/home.cfm$ /test/index.cfm?page=home%1 [NC,R=301,L]
I think ISAPI Rewrite is a little different to mod_rewrite:
Whenever you put parentheses in any regular expression present in a complex rule (a rule with conditions) you are marking a submatch that could be used in a format string (using $N syntax) or as a back-reference (using \N syntax) in subsequent conditions. These submathces are global for the whole complex rule (RewriteRule directive and corresponding RewriteCond directives). Submatches are numbered from up to down and from left to right beginning from the first RewriteCond directive (if such directive exists) corresponding to the RewriteRule.
So try $1 to get the match of the first group no matter where it appears:
RewriteCond %{QUERY_STRING} ^page=default(.*)$ [I]
RewriteRule ^/test/home\.cfm$ /test/index.cfm?page=home$1 [I,R=301]
ISAPI Rewrite 3 seems to be more compatible to Apache’s mod_rewrite.
I believe this works also:
RewriteRule ^/test/home.cfm\?page=default(.*)$ /test/index.cfm?page=home%1 [I,R=301]

htaccess rewrite rule doesn't work with dashes?

my htaccess rule isn't working with rewrite with dashes in:
RewriteRule ^([A-Za-z]+)$ index.php?do=$1 [QSA]
so, www.domain.com/rules works, however, www.domain.com/about-us doesn't
I've verified that www.domain.com/index.php?do=about-us works so it's definately a rewrite issue.
Thanks.
Your regular expression doesn't include a check for dashes - try:
RewriteRule ^([A-Za-z\-]+)$ index.php?do=$1 [QSA]
Your regex only takes a-z and A-Z, change it to [A-Za-z\-] so it will include the - character

Resources