RewriteRule for dropping a specific string in a URL

Discuss practical ways rearrange URLs using mod_rewrite.

RewriteRule for dropping a specific string in a URL

Postby bb8011 » Thu Oct 02, 2008 11:12 am

Is it possible to craft a RewriteRule that drops a particular string in a URL?

For instance, I'd like to get:

http://foobar.org/cgi/i/image/image-idx ... =1;resnum=

to drop the "id=S-DJG-X-82.3%20WS82_3B;" but keep the rest of the URL string so ends up looking like this:

http://foobar.org/cgi/i/image/image-idx ... =1;resnum=

Yes, I know this URL is ugly long regardless.

I have tried what I can to do the above--but am fairly new to regexpressions and mod_rewrite. Any suggestions or alternative methods would be extremely helpful.
bb8011
 
Posts: 5
Joined: Thu Oct 02, 2008 7:49 am

Postby richardk » Thu Oct 02, 2008 2:10 pm

Just "id=S-DJG-X-82.3%20WS82_3B;" or all "id="s?
Where are you putting the mod_rewrite?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?id=S-DJG-X-82.3%20WS82_3B;(&.*)?$ [NC]
RewriteRule ^(cgi/i/image/image-idx)$ /$1?%1%2 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby bb8011 » Fri Oct 03, 2008 5:10 am

Richard

Sorry, let me start over...

I already have a mod_rewrite placed in an .htaccess file that includes a redirect. Currently this is what it looks like:

Code: Select all
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /cgi/i/image/
RewriteRule (IC-(.*)-X-(.*)) image-idx?id=S-$2-X-$3 [R,L]


This is to redirect a short URL we use for bookmarking. Once redirected, the context of "id=whatever" needs to be dropped from all future URLs--otherwise trying to use some tools on the page that generate new URLs end in an error.

So, to clarify, I'm looking to drop all "id="s. And I hope this makes sense.

Thanks for your help--you seem to be one of the most active and able to solve many problems people are having on here.
bb8011
 
Posts: 5
Joined: Thu Oct 02, 2008 7:49 am

Postby richardk » Fri Oct 03, 2008 10:43 am

This is to redirect a short URL we use for bookmarking. Once redirected, the context of "id=whatever" needs to be dropped from all future URLs--otherwise trying to use some tools on the page that generate new URLs end in an error.

So, to clarify, I'm looking to drop all "id="s. And I hope this makes sense

It will also remove it from the redirect request (image-idx?id=S-$2-X-$3) unless there's something specific about it that can be matched.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteBase /cgi/i/image/

RewriteRule (IC-(.*)-X-(.*)) image-idx?id=S-$2-X-$3 [R,L]

RewriteCond %{QUERY_STRING} ^(.*&)?id=[^&]+(&.*)?$ [NC]
RewriteRule ^image-idx$ image-idx?%1%2 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby bb8011 » Mon Oct 06, 2008 5:54 am

Richard:
Tried your well thought out suggestion, unfortunately it isn't working.

The redirect from

Code: Select all
RewriteRule (IC-(.*)-X-(.*)) image-idx?id=S-$2-X-$3


still works too.

Thanks for your help, I will continue pounding away at this and hopefully come up with some solution.
bb8011
 
Posts: 5
Joined: Thu Oct 02, 2008 7:49 am

Postby richardk » Mon Oct 06, 2008 11:23 am

What URL are you visiting? What should happen to it? (Visible redirection? To what?)

Where is the .htaccess file?

Try replacing
Code: Select all
^image-idx$

with
Code: Select all
^(.+/)?image-idx$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby bb8011 » Mon Oct 06, 2008 11:47 am

What URL are you visiting? What should happen to it? (Visible redirection? To what?)


When visiting
http://digital.pk.wayne.edu/cgi/i/image ... =1;resnum=

There is no visible redirection to anything. The URL remains the same, rather than a redirect to the URL we are attempting to land at:

http://digital.pk.wayne.edu/cgi/i/image ... =1;resnum=


Where is the .htaccess file?


Its placed in the server's Document Root.

To reply to your last post, when I replace

Code: Select all
^image-idx$


with

Code: Select all
^(.+/)?image-idx$


The same happens--no visible redirect takes place and the server lands on

http://digital.pk.wayne.edu/cgi/i/image ... =1;resnum=

If you'd like to visualize this, step by step, you can first visit:

http://digital.pk.wayne.edu/IC-VMC-X-3007%5D3007

Once page redirects, try enlarging the image by clicking the box below the one that is filled in red.

Thanks again and apologies for my lack of clarity.
bb8011
 
Posts: 5
Joined: Thu Oct 02, 2008 7:49 am

Postby richardk » Mon Oct 06, 2008 1:29 pm

Is /cgi a ScriptAlias?
Is the /cgi directory in your document root?

Try the following in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^IC-(.+-X-.+)$ /cgi/i/image/image-idx?id=S-$1 [R,L]


And this in /cgi/i/image/
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?id=[^&]+(&.*)?$ [NC]
RewriteRule ^image-idx$ /cgi/i/image/image-idx?%1%2 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby bb8011 » Tue Oct 07, 2008 5:16 am

/cgi is not in the document root and is a ScriptAlias
Code: Select all
ScriptAlias           /cgi/ /l1/dlxs/cgi/


Placing
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^IC-(.+-X-.+)$ /cgi/i/image/image-idx?id=S-$1 [R,L]

in document root works with a visible redirect, however
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?id=[^&]+(&.*)?$ [NC]
RewriteRule ^image-idx$ /cgi/i/image/image-idx?%1%2 [R=301,L]


in cgi/i/image still gives same error result. I'm guessing this is something to do with the /cgi ScriptAlias?
bb8011
 
Posts: 5
Joined: Thu Oct 02, 2008 7:49 am

Postby richardk » Tue Oct 07, 2008 11:31 am

Does any mod_rewrite work in /l1/dlxs/cgi? Try this mod_rewrite test. You might not have mod_rewrite/.htaccess files enabled in it.

Mod_rewrite in your document root does not run for Aliases or ScriptAliases. The request goes directly to the Alias directory skipping the document root.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 34 guests

cron