Parsing Mod-Rewrite - mod-rewrite

I'm trying to get two parameters with mod-rewrite. I tried to split them with "-" but unfortunately it returns last word as second parameter.
/ders/ilkogretim-matematik
/ders/ilkogretim-fen-ve-teknoloji
should be the URLs, "ilkogretim" will be the first parameter and the rest of it will be the second parameter. (After first "-")
My rules as follows:
RewriteRule ^ders/(.*)-(.*)/?$ /ogretmenler.php?sinif=$1&ders=$2 [QSA,L]
I hope I could explain the problem..
Thanks in advance...

Your . is only capturing a single character - you need a quantifier on there.
I've also made the first group capture any character except -:
ders/([^-]+)-(.*)/?$ /ogretmenler.php?sinif=$1&ders=$2 [QSA,L]

The problem is the single dots (.)-(.) will only match a single character. You probably want something like
^/ders/([^-]*)-(.*)/?$
The first group will match zero or more non - characters, followed by the single - and then the 2nd group will match zero or more of any character (you could restrict this more if desired).

Related

Regex Expression for Hashtags

/(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))(\w+)(?=\s|$)/i
This is the current expression i use. It works fine except the dash - is not allowed. I need this: #what-ever to be captured.
How can i add the dash to this expression ?
Just add the pattern which was present inside the first capturing group that is \w plus - into a character class. So that it would capture a word character or a - symbol. + after the character class makes the previous token to repeat one or more times.
(?:\s|^)(?:#(?!(?:\d+|\w+?_|_\w+?)(?:\s|$)))([-\w]+)(?=\s|$)
|here|
DEMO

Firefox showing an odd string of characters in the location bar

I've seen this a few times recently, but never paid attention to it. Now that it's causing problems, though I need to figure it out.
Sometimes when I visit a site in Firefox 13 on the Mac I get this strange string attached to the end of the URL in the location bar.
It always starts with "#.T_" and is usually something like "#.T_3HiHDevA8".
For the life of me I can't figure out what's causing it. It only comes up on the first load
I do have a bit of rewriting going on, but I don't understand how that could be adding things to the location bar. Here's my .htaccess file.
RewriteCond %{REQUEST_URI} !=/
RewriteRule ^([a-zA-Z_]+)/?$ / [QSA]
Thanks for any help.
'qsappend|QSA' (query string append)
This flag forces the rewrite
engine to append a query string part of the substitution string to the
existing string, instead of replacing it. Use this when you want to
add more data to the query string via a rewrite rule.
The QSA flag indicates to append the query string of the subsitution string.
Taking a futher look:
^([a-zA-Z_]+)/?$
^ = Beginning of URL
( ) = Groups a series of pattern elements
[ ] = Denotes a set of possible matches
a-zA-Z_ = Alpha-numerical characters and underscore '_' only
+ = indicates there is one or more of the preceding element
/ = Forward slash in URL
? = Match preceeding 0 or 1 times
$ = End of URL
Apply the above logic to your rule: RewriteRule ^([a-zA-Z_]+)/?$ / [QSA] will redirect the matches per the breakdown above to '/' and append the query string, assuming the URL does not eqaul to '/'.

Rewriting URLs with mod_rewrite for languages

I need some URL rewriting for my website using mod_rewrite but I can't figure out the regular expressions.
Here is what the current URLs may look like:
http://mydomain.com/zenphoto/pages/xyz?locale=en_US
http://mydomain.com/zenphoto/pages/xyz?locale=de_DE
http://mydomain.com/zenphoto/gallery_1?locale=de_DE
http://mydomain.com/zenphoto/gallery_n?locale=de_DE
xyz may contain different strings, e.g. legal, about, etc.
And that's how I'd like the URLs to be used:
http://mydomain.com/zenphoto/de/pages/xyz
http://mydomain.com/zenphoto/en/pages/xyz
http://mydomain.com/zenphoto/de/gallery_1
http://mydomain.com/zenphoto/en/gallery_n
I should mention that only de and en shall be possible. Any other strings shall be rerouted to de.
Could somebody help me please? :-)
Thanks,
Robert
RewriteEngine on
RewriteRule ^zenphoto/pages/([a-z]+)\?locale=(en|de)_[A-Z]{2}$ /zenphoto/$2/pages/$1
RewriteRule ^zenphoto/gallery_([0-9])\?locale=(en|de)_[A-Z]{2}$ /zenphoto/$2/gallery_$1
For the first example, I say: "If the URL starts (^) with "zenphoto/pages/" then have a sequence of lowercase letters (+ means "one or more", and [a-z] means "a letter in [a, b, ..., y, z]"), which is my first group (there is parentheses -> it's a group). Then it's followed by "?locale=", then by "en" or (| means "or") "de", and this is my second group, then it's followed by an underscore ("_") and two uppercase letters, and there is nothing after ($ means it's the end of the URL)".
I write a space, and the new URL I want, and I use $n to use the n-th group.
The second URL is the 'pretty one', and the first is the real.
You have to use backslashes before special chars like ?,+,{,},(,),[,],*,.,| if you want to use one in your URL.
Edit:
If you want to avoid infinite loops, you should add the flag [L] (L = Last) at the end of each line.

Getting last part of URL into variable with Url Rewriter

I'm using Url Rewriter to create user-friendly URLs in my web app and have the following rule set up
<rewrite url="/(?!Default.aspx).+" to="/letterchain.aspx?ppc=$1"/>
How do I replace $1 so that it is the last part of the URL?
So that the following
www.mywebapp.com/hello
would transform to
/letterchain.aspx?ppc=hello
I've read the docs but can't find anything.
The $1 in the to portion of the group refers to the first capture group defined (eg the part in the brackets).
The part that you actually want injecting into the $1 is the .+ which isnt in a capture group.
I'm not sure but I think because of the (?! ) "match if suffix is absent" query this isnt counted as numbered capture group $1 so this should work:
<rewrite url="/(?!Default.aspx)(.+)" to="/letterchain.aspx?ppc=$1"/>
If it doesnt then just try inserting the second capture group into your to string instead:
<rewrite url="/(?!Default.aspx)(.+)" to="/letterchain.aspx?ppc=$2"/>
Please note that if you are developing for IIS 7+ http://www.iis.net/download/urlrewrite/ is a module from Microsoft that performs faster rewrites with lower footprint.
BTW, your regex has a small problem, you need to escape the dot character, that is "/(?!Default.aspx)(.+)"

ReWrite RegEx, URL having at least one character

I have the following RewriteRule:
RewriteRule ^/people/([A-Za-z0-9\-\_]*)/?$ /people/people_details.cfm?person=$1 [I,L]
...it works great for forwarding my rule, but I want to make sure that the regex only picks it up if it has more than one character. So really, I need to have my regex...
[A-Za-z0-9\-\_]+
...have an additional rule to say that there has to be at least one character. Right now if I go to...
/people/
...it should go to the default document index.cfm, but because of the rule, it still tries to forward to my people_details.cfm
Any help?
Thanks,
George
Your regular expression that you put in your question already ensures that there must be at least one character. The + means "1 or more", as opposed to * which means "zero or more". Just change the * to a +.
...it should go to the default document index.cfm, but because of the rule, it still tries to forward to my people_details.cfm
Thats because you have the "/" as optional at the end, which is probably not what you wanted.

Resources