Forum Moderators: phranque
http://www.myserver.com/_some_random_text/optional_subdirectory/page.html
http://www.myserver.com/optional_subdirectory/page.html
http://www.myserver.com/optional_subdirectory/page.html
RewriteCond %{REQUEST_URI} ^(.*)/_([^/]+)/(.*)$
RewriteRule ^(.*)/_([^/]+)/(.*)$ $1/$3
RewriteCond %{REQUEST_URI} ^(.*)(/_[^/]+)/(.*)$
RewriteRule ^(.*)(/_[^/]+)/(.*)$ $1/$3
Welcome to WebmasterWorld [webmasterworld.com]!
Where do you intend to place the rewrites, in http.conf, or in .htaccess?
Do you have other RewriteRules working already?
Jim
Here is my new regex that works fine in python and matches perfectly
^(.*?)(/_(.*?))(/.*?)$
RewriteCond: cannot compile regular expression '^(.*?)(/_(.*?))(/.*?)$'
You only need to worry about 'greedy' vs. 'non-greedy' when you are trying to match parts of the URL that look identical to the pattern, but you want to put, say, one of the identical parts into the first backreference, and two into the second... Or something similar to that. In that case, you need to be careful with using ".*" to match substrings, because ".*" is greedy, and will match as much of the string as possible, possibly "eating up" more than you expect. In the example at hand, if you used if for the first back-reference, it might match *two* of the identical substrings, leaving only one (or none) to match in the second back-reference. Since this is sort of a secondary topic, I'll leave it at that.
It looks to me like you had a problem with defining the 'borders' between the optional and non-optional parts of the requested URL -- matching the 'required' versus the 'optional' slashes, in particular.
Try this in.htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)(/_[^/]+)?/(.+)$ /$1/$3 [L]
To help get it working, you may want to use an external redirect at first, so you can see the result in your browser address bar:
RewriteRule ^([^/]+)(/_[^/]+)?/(.+)$ /$1/$3 [L] http://www.example.com/$1/$3 [R=301,L]
I'd recommend trying very simple rewrites first, and getting those working before diving into complex regular-expressions rewrites. Something like:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /index.html [L]
Jim,
RewriteEngine On
RewriteRule ^(.+/)?(_[^/]*)/(.*)$ $3 [L]
/Users/username/Sites/site1/test.php
http://localhost/~username/site1/_test/test.php
/Users/username/Sites/site1/test.php
File does not exist: /Library/WebServer/Documents/Users/username/Sites/site1/test.php
RewriteEngine On
RewriteRule ^(.+/)?(_[^/]*)/(.*)$ $1 [L]
File does not exist: /Library/WebServer/Documents/Users/username/Sites/site1/
RewriteEngine On
RewriteRule ^(.+/)?(_[^/]*)/(.*)$ http://localhost/~username/site1/$3 [L]
I noticed in my /etc/httpd/httpd.conf that i have the following lines
<Directory "/Library/WebServer/Documents">
<IfModule mod_userdir.c>
UserDir Sites
</IfModule>
Thanks Again
Wing
I'm not sure, but your setup for UserDir [httpd.apache.org] doesn't look right... It needs a directive with the keyword "enabled" in there, too.
You also might try putting the whole path (from Apache root down) in there.
For yet another layer of complexity, see the RewriteBase [httpd.apache.org] directive in mod_rewrite.
<added>
From message #4 above:
I'd recommend trying very simple rewrites first, and getting those working before diving into complex regular-expressions rewrites.Use a simple filename-only rewrite to get your UserDir and RewriteBase set up properly before attempting to rewrite directories - it will save you a lot of time.
Jim
A little further down is
Include /private/etc/httpd/users/*.conf
<Directory "/Users/username/Sites/">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /Users/username/Sites/site1
RewriteRule ^now\.php$ index.php [L]
File does not exist: /Library/WebServer/Documents/Users/username/Sites/site/index.php
RewriteBase /Users/username/Sites/site1/anotherrandomsubfortesting
File does not exist: /Library/WebServer/Documents/Users/username/Sites/site/anotherrandomsubfortesting/index.php
Thanks Again
Wing
RewriteBase /~username/site1
RewriteBase /Users/username/Sites/site1
which would be where the docbase is
Thanks for all the help!
Wing Lian
P.S
Just in case anybody wants the final code, here goes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /~username/site1
RewriteRule ^(.+/)?(_[^/]*)/(.*)$ $1$3 [L]