Forum Moderators: phranque
Being quite new to regular expressions, I am hoping that someone is able to validate my rewrite rule.
What it should do is ignore any URLs except those that have a /cv/ in them.
e.g. IGNORE: www.example.com/something/somepage.html
e.g. IGNORE: www.example.com/cv.php
e.g. PARSE: www.example.com/cv/company/user
It should parse the following:
www.example.com/cv/company/user
to:
www.example.com/index.php?cv=1&cp=company&cd=user
Here is my .htacess:
RewriteEngine on#RewriteRule^[^/cv/]+/cv/([^/]+)/(.+)$ /index\.php?cv=1&cp=$1&cd=$2
In plain text my rule is (correct me if I am wrong):
^ from the start
[^/cv/]+ find one or more characters that AREN'T '/cv/'
/cv/ followed by '/cv/'
([^/]+) followed by one or more characters that arent a slash
/ followed by a slash
(.+) followed by everything else (must be at least one)
[edited by: jdMorgan at 3:47 pm (utc) on April 8, 2005]
[edit reason] Examplified. [/edit]
(correct me if I am wrong)
Ok. =)
You've got the right idea, but you're missing a couple of tidbits on how regexes work, and you're making it harder than it actually is (something I'm famous for. In *my* head, anyway).
So, four things to know:
[]
RewriteEngine on
RewriteRule ^/cv/([^/]+)/(.+)$ /index.php?cv=1&cp=$1&cd=$2 [L]
Make sense?
My latest errors (after trying http://www.example.co.nz/cv/a/b) are:
[Fri Apr 8 15:57:16 2005] [error] [client 222.***.211.187] File does not exist: /home/example/public_html/cv/a/b
This seems to me like either non of the rules are working (i.e. rewrite is not enabled) or this rule isn't. What is an easy way to check?
[edited by: jdMorgan at 3:46 pm (utc) on April 8, 2005]
[edit reason] Obscured specifics per TOS. [/edit]
(see the first couple of paragraphs at [httpd.apache.org ] for a rather detailed explanation on this)
If the .htaccess file is your only option, the syntax changes ever-so-slightly, and some prerequisites must by fulfilled by the server administrator:
RewriteEngine on
RewriteRule ^cv/([^/]+)/(.+)$ /index.php?cv=1&cp=$1&cd=$2 [L]
To test whether the prerequisites have been met, the easy solution is to create a simple RewriteRule:
RewriteEngine on
RewriteRule ^.* http://www.google.com/ [L,R]
Its definatly getting there (leaving off the first slash), however, images are treating the base URL as the url was BEFORE the rewrite rule. Is there any way to force apache to cause a proper 'redirect' so to speak?
For example,
www.example.com/index.php
The root is : www.example.com/
However, with this rewrite rule:
www.example.com/cv/one/two -> www.example.com/index.php?cv=1&cp=one&cn=two
The root is : www.example.com/cv/one/
So images are attempting to be loaded from:
www.example.com/cv/one/images/picture.jpg
Rather than:
www.example.com/images/picture.jpg
Cheers,
Peter
[edited by: jdMorgan at 3:50 pm (utc) on April 8, 2005]
[edit reason] Examplified. [/edit]
Also, read this thread; wrote:
RewriteEngine on
RewriteRule ^/cv/([^/]+)/(.+)$ /index.php?cv=1&cp=$1&cd=$2 [L]
in .htaccess file
and, this in 'index.php' file:
<a href="http://www.example.com/index.php?cv=1&cp=company&cd=user">Hover here</a>
<br />
<a href="index.php?cv=1&cp=company&cd=user">Hover here again</a>
But, when I called http://localhost/index.php in url; there is NO! again :¦
[edited by: jdMorgan at 4:00 pm (utc) on April 8, 2005]
[edit reason] Examplified. [/edit]
Its definatly getting there (leaving off the first slash), however, images are treating the base URL as the url was BEFORE the rewrite rule. Is there any way to force apache to cause a proper 'redirect' so to speak?
You've got at least three options, although I suspect one of them is unworkable for you (substitute your server's documentroot where you see $DOCROOT):
RewriteRule ^cv/[^/]+/images/(.*) /images/$1 [L]
Alternatively, you could use:
Alias /cv/one/images $DOCROOT/images
Personally, I'd go with the symlink; it's easy and there's zero additional server overhead involved. If you want to document the reason for the symlink someplace, you could drop a comment in the .htaccess file so you have it written down in case you (or someone else, if more than one of you work on this site) wonder what the heck it's doing there in a year or two. You could even create a file called something like REASON_FOR_SYMLINK, put docs in there, and then chmod the file mode 600 so that the webserver can't read it (or, you could just maintain documentation somewhere else. Options galore!)