Forum Moderators: phranque
Here is a sample of the current structure:
my_site.com/catalog.html/purple_widgets/3
This is how I'd like it to appear:
my_site.com/purple_widgets/3.html
Could someone please help me out with this. I know it's best to learn to catch fish, instead of asking for a fish but if you give me a fish(rewrite code:)) for this problem I'll learn to catch my own in the future.
Thanks
[httpd.apache.org...]
[httpd.apache.org...]
[etext.lib.virginia.edu...]
One of our regular mod_rewrite wizards will probably be along soon to offer suggestions.
This is how I want the urls to appear:
/red_widget_catalog.html
and
/red_widget_info/1.html
Then be rewritten to:
/widget_catalog.html/red_widgets
and
/widget_info.html/1
RewriteEngine On
RewriteRule ^(.*)_widget_catalog\.html$ /widget_catalog\.html/$1
RewriteRule ^(.*)_widget_info/([0-9+])\.html$ /widget_info\.html/$1/$2
Disregard the first post. I believe this is the structure I would prefer.
I think the the expression on the right needs the variable added differently as in:
RewriteRule ^(.*)_widget_catalog\.html$ /$1_widget_catalog.html
not sure though, don't recall ever seeing this type rule...
Setup a test page and try it on your server.
RewriteRule ^(.*)_widget_catalog\.html$ /$1_widget_catalog.html
Does the first part(bold) grab everything before _widget_catalog, including the http://
The string I really want to get is ht*p://www.mysite.com/red_widget_catalog.html
Thanks again for the help. I'm starting to get it:)
As DaveAtIFG pointed out,
The expression on the left is a regex pattern, the expression on the right is a literal destination...
The string on the left starts in your top-level directory file path, not with "http://www.yourdomain.com".
The string on the right can start with "http://www.yourdomain.com", but unless you want to redirect off-site, leave it off.
Thanks for the help, everyone. One more question:
RewriteRule ^(.*)_widget_catalog\.html$ /$1_widget_catalog.html
Does the first part(bold) grab everything before _widget_catalog, including the [<...]If your RewriteRule is located in a .htaccess file in your top-level (web-visible) directory, then "^(.*)" grabs everything in the file path up to "_widget_catalog...", starting after "http://www.yourdomain.com/". If the RewriteRule is located in .httpd (above the directory levels accessible from the web) then "^(.*)" grabs the slash as well, as in "/red" in your example.
The parentheses around ".*" mean that it is either a text grouping or that you wish to back-reference the contents using $1, $2, etc. - It looks like you've got that concept.The string I really want to get is ht*p://www.mysite.com/red_widget_catalog.htmlThe RewriteRule will be applied to any method (e.g. http, https) and any domain that "lands" in your top level directory or below. If you need to exclude certain methods or domains from being affected by a RewriteRule, you can use RewriteCond to exclude them. The system variables %{REQUEST_METHOD} and %{HTTP_HOST} will pick up the HTTP vs. HTTPS, etc., and the domain name used in the request, respectively.
It looks like you're basically on the right track here. A couple of tweaks to make your rules correspond with what you said you wanted to accomplish (assuming you're doing this in .htaccess):
RewriteEngine On
# rewrite /red_widget_catalog.html -> /widget_catalog.html/red_widgets
RewriteRule ^(.*)_widget_catalog\.html$ /widget_catalog.html/$1_widgets
#
# rewrite /red_widget_info/1.html -> /widget_info.html/1
RewriteRule .*_widget_info/([0-9+])\.html$ /widget_info.html/$1
# (The start anchor and first set of parentheses are not needed in this case)
HTH,
Jim