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

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

Related

Rewrite rule 301 with different formatting

Okay, so I need to find the correct syntax for what I want to do.
These links:
http://www.example.com/sub1/sub2/product1/
(...)
http://www.example.com/sub1/sub2/product700/
need to redirect to
http://www.example.com/sub3/product1-newsite/
(...)
http://www.example.com/sub3/product700-newsite/
What I have tried:
RewriteEngine On
RewriteRule ^/sub1/(.*)/(.*)$ http://www.example.com/subdir/$1-newsite/ [R=301,L]
For all the 700 products.
Also I need to make exeptions for certain products.
Can someone see what I'm doing wrong?
I think it's just the numbering on the regular expression that is an issue.
$1 is the first matched group, $2 is the second, etc. $0 is for the entire string that matched (ie. the whole path).
Try:
RewriteEngine On
RewriteRule ^/sub1/sub2/(.*)$ http://www.example.com/subdir/$1-newsite/ [R=301,L]
or
RewriteEngine On
RewriteRule ^/sub1/(.*)/(.*)$ http://www.example.com/subdir/$2-newsite/ [R=301,L]
Edit
Ah... had a thought... the first regex in the capture may be being a bit too greedy... since it can match:
^/sub1/[sub2/product1-newsite]/[] so $1 will be sub2/product1-newsite and $2 will be empty (.* can happily match nothing).
So we need to make sure it doesn't grab too much.
RewriteEngine On
RewriteRule ^/sub1/([^/]+)/(.+)/$ http://www.example.com/subdir/$2-newsite/ [R=301,L]
Changing the * to + makes sure we match at least one character.
Changing the . to [^/] makes sure we match "anything except a /".
Added a / on the end to make sure the $2 doesn't capture the trailing slash.
That should hopefully fix it.

Can we go beyond $1 - $9 positional parameters with mod_rewrite

Is there any way to use more than the $1 to $9 positional parameters with mod_rewrite? ${10} does not work.
In apache's RewriteRule backreference definition it says:
RewriteRule backreferences: These are backreferences of the form $N (0 <= N <= 9), which provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions.
However, you might be able to match some of the URI in a RewriteCond and use the %N backreferences, I've never tried it myself but maybe something like this?
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/
RewriteRule ^[^/]+/[^/]+/[^/]+/[^/]+/[^/]+/[^/]+/[^/]+/[^/]+/[^/]+/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /ok_wow?a=%1&b=%2&c=%3&d=%4&e=%5&f=%6&g=%7&h=%8&i=%9&j=$1&k=$2&l=$3&m=$4&n=$5&o=$6p=$7&q=$8&r=$9 [R]
(yeah, that's pretty absurd, recursion is probably a better way to go if it's applicable).

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 ?

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

why doesn't this rewrite the url properly?

RewriteCond %{REQUEST_URI} !^/?new.php?url
RewriteRule ^/?(.+)\.?(.+)$ new.php?url=$0 [L]
its supposed to take any URL
mysite.com/someurl
and convert it to
new.php?url=someurl
however it keeps going to just new.php
You need to escape the second question mark in the first line so it matches a literal question mark:
RewriteCond %{REQUEST_URI} !^/?new.php\?url
Also you are not using the parenthesized groups on the second line. $0 is okay, or you may want $1 instead. If you use $0 you could simplify it a bunch:
RewriteRule ^.*$ new.php?url=$0 [L]
Or on the other hand if you're breaking apart the URL for a reason I would suggest some fixup. You're not matching the file name and extension exactly right. A little more complex regex like this would probably do you better:
RewriteRule ^/?(.*?)(?:\.([^.]*))?$ new.php?path=$1&extension=$2 [L]
Explanation:
(.*?) matches the directory and file name. The ? means match non-greedily, so stop as soon as the next part matches. The parentheses cause it to be captured and stored in $1.
(?:\.([^.]*))? matches the file extension. ?: turns says to not capture the outer set of parentheses, so the dot is not captured in $2. ([^.]*) matches the extension and ensures that it does not contain a dot. The final ? makes the file extension part optional, just cause not all URLs have file extensions. Thus there will only be a $2 if there is a file extension.
The first back-reference is $1 not $0 AFAIK. If that doesn't do it try specifying [QSA] as well, though I doubt that's it.
RewriteCond %{REQUEST_URI} !^/?new.php?url
RewriteRule ^/?(.+)\.?(.+)$ new.php?url=$1 [L]
The first back reference should be $1 instead of $0.
RewriteCond %{REQUEST_URI} !^/?new.php?url
RewriteRule ^/?(.+)\.?(.+)$ new.php?url=$1 [L]
It also depends on what other lines of code you have in the file. It's also possible to mess up rewrites if you have another code that conflicts with it.

Resources