Forum Moderators: phranque
RewriteRule ^/(.*)/?$ /pages.php?s=$1 [L]
RewriteRule ^/([^/]+)/(.*)/?$ /pages.php?s=$1&p=$2 [L]
RewriteRule ^/([^/]+)/([^/]+)/(.*)/?$ /preview.php?s=$1&p=$2&i=$3 [L]
also
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)/([0-9]+)/?$ /preview.php?s=$1&p=$2&i=$3
RewriteRule ^([A-Za-z]+)/([A-Za-z]+)/?$ /pages.php?s=$1&p=$2
RewriteRule ^([A-Za-z]+)/?$ /pages.php?s=$1
I have tried with ^/ and with simply ^. With and without a $ terminating the match (? instead of?$ as well as simply $). Using (/)? for the trailing slash. Using [A-Za-z]* instead. With and without [L] with and without [R] with and without [NC]. Etc. etc. etc. I have tried inserting the domain in front of the parts to match.
I have also tried myriad variations on the following:
RewriteCond %{REQUEST_URI}!^([A-Za-z]+)/([A-Za-z]+)/([0-9]+)(/)?$
RewriteRule /preview.php?s=$1&p=$2&i=$3 [L]
RewriteCond %{REQUEST_URI}!^([A-Za-z]+)/([A-Za-z]+)(/)?$
RewriteRule (.*)/pages.php?s=$1&p=$2 [L]
RewriteCond %{REQUEST_URI}!^([A-Za-z]+)(/)?$
RewriteRule (.*)/pages.php?s=$1 [L]
I have scoured forums and tutorials trying every small change. My syntax has not appeared to matter, I am getting a 404 file not found error, both on my localhost and on my site online.
I have URLS currently [site.com...]
or
[site.com...]
or
[site.com...]
I want to instead allow:
[site.com...] (Item #)
I typically hate posting forums on questions that have undoubtably been answered 1,000 times, but I am incredibly frustrated at this point. I've been working on this for 3.5 hours.
By the way, the file I'm being told doesn't exist on my local machine is G:\Apache\Apache\htdocs\VR2\Section ... which isn't SUPPOSED to exist.
Welcome to WebmasterWorld!
Your first set of rules is most appropriate. However,
1) Your RewriteRules are in reverse order. Put the most specific rule (in this case, the one with the longest pattern) first.
2) You may have an error or omission in defining DocumentRoot in httpd.conf. DocumentRoot should define the 'base' directory of your Web site, not the path to Apache.
3) If DocumentRoot cannot be changed for some 'site architecture' reason, look into using the RewriteBase directive; You would put the path /htdocs/VR2/ (or more or less) into RewriteBase.
4) For any rule where the new URL may match the pattern of any other rule, you'll need to exclude that new URL from being rewritten (mod_rewrite behaves as if it's recursive in an .htaccess context). The [L] flag only applies within the context of the current mod_rewrite 'run' and so will not prevent recursion of the entire set of rules.
5) In an .htaccess context, omit the leading slash from your RewriteRule patterns. Include the leading slash for RewriteCond %{REQUEST_URI} patterns, though.
See the online Apache documentation for more info.
Jim
RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/?$ /preview.php?s=$1&p=$2&i=$3
RewriteRule ^(.*)/(.*)/?$ /pages.php?s=$1&p=$2
RewriteRule ^(.*)/?$ /pages.php?s=$1
RewriteEngine off
Taking the advice from you and 1 other person. In the rules with more than one match, should I be using ([^/]+) as per your example in the thread mod_rewrite charity? I noted though the leading slash was omitted in the "match" it was not omitted in what we want to rewrite to (ie: /pages.php).
I thought that RewriteBase would help a lot, given the situation of virtual hosts being set up, but trying:
apache/apache/htdocs/vr2
apache/htdocs/vr2
htdocs/vr2
Gave me the error:
g:/apache/apache/htdocs/vr2/.htaccess: RewriteBase: argument is not a valid URL
When I got down to:
/vr2
It went back to:
File does not exist: g:/apache/apache/htdocs/vr2/compose
The same problem is occuring when I upload it to my host and try. I haven't looked at that error log, but I get my 404 page. (That's the reason why I immediately tried RewriteBase...it might be b/c of the Document Roots set up in the virtual hosts).
I'm about to go read the Apache manual again, but I quite identify with the quote about voodoo. It's all mystical sounding to me. I don't quite understand the point you are making in in your advice #4.
Thank you, however.
RewriteEngine on
# Require 3 parameters for "preview" rewrites
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /preview.php?s=$1&p=$2&i=$3 [L]
# Require 2 parameters for two-query-variable "pages" rewrite
RewriteRule ^([^/]+)/([^/]+)/?$ /pages.php?s=$1&p=$2 [L]
# Accept one (or blank) parameter for "pages" rewrite, but do not rewrite "pages.php" itself!
RewriteCond %{REQUEST_URI} !^/pages\.php$
RewriteRule ^(.*)/?$ /pages.php?s=$1 [L]
# RewriteEngine off (DELETE THIS - Not needed except to disable rules that follow it)
Recursion: This rule will loop until the maximum redirction limit is reached:
RewriteRule ^(.*)/?$ /pages.php?s=$1
Jim
RewriteEngine on
# Require 3 parameters for "preview" rewrites
RewriteRule ^i/([^/]+)/([^/]+)/([^/]+)/?$ /preview.php?s=$1&p=$2&i=$3 [L]
# Require 2 parameters for two-query-variable "pages" rewrite
RewriteRule ^p/([^/]+)/([^/]+)/?$ /pages.php?s=$1&p=$2 [L]
# Accept one (or blank) parameter for "pages" rewrite, but do not rewrite "pages.php" itself!
RewriteCond %{REQUEST_URI} !^/pages\.php$
RewriteRule ^(.*)/?$ /pages.php?s=$1 [L]
RewriteEngine off
(trying to prevent extraneous matching... url [localhost...] should be translated to [localhost...] gives the error:
[Sat Jan 29 21:26:27 2005] [error] [client 127.0.0.1] File does not exist: g:/apache/apache/htdocs/vr2/i/Design/Web-Layouts/2
Upload to the web server using the url [example.net...] gives a 404 error.
I'm giving up. 5 hours of my Saturday wasted. Thank you for all your help. No one should have to do my "work" for me by writing the rules out. I figure if yours doesn't work, and 5 hours of my attempts haven't worked, ... I am lost! I appreciate you having done so much to help.
[edited by: jdMorgan at 5:03 am (utc) on Jan. 30, 2005]
[edit reason] Removed specifics per TOS. [/edit]
The URL-path /pages.php should resolve to wherever your Web root directory is (where your "home page" goes). So this is obviously now a config problem, not an .htaccess problem. I just wanted to correct the rules so that when you get the config set up correctly, they'll work right away -- or at least be very close to what you wanted.
Good luck with your project.
Jim