homepage Welcome to WebmasterWorld Guest from 54.211.181.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
redirect to truncate characters
pemba76

5+ Year Member



 
Msg#: 4293624 posted 10:10 am on Apr 7, 2011 (gmt 0)

I have the following working rule :
RewriteRule (.*)-sample/id/([^/]+)$/(.*) [mysite.com...] [R=301,L]

The above code helps me add a slash to the end of the URL
Eg1 : [mysite.com...] changes to [mysite.com...]
Eg2 : [mysite.com...] changes to [mysite.com...]

But when i type [mysite.com...] , the URL does not change to [mysite.com...]
The page display right content but the URL is wrong - [mysite.com...]

How do i automatically truncate all 'rubbishcharacter213342' (any character after the last slash) from the URL automatically and change URL to [mysite.com...]

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4293624 posted 7:26 pm on Apr 7, 2011 (gmt 0)

Please repost the details and use "example.com".
The post is unreadable in the present form.
The forum will not auto-link example.com URLs.

pemba76

5+ Year Member



 
Msg#: 4293624 posted 4:45 am on Apr 8, 2011 (gmt 0)

I have the following working rule :
RewriteRule (.*)-sample/id/([^/]+)$/(.*) http://www.example.com/$1-sample/id/$2/ [R=301,L]

The above code helps me add a slash to the end of the URL
Eg1 : http://www.example.com/INK-sample/id/70001452 changes to http://www.example.com/INK-sample/id/70001452/
Eg2 : http://www.example.com/FILTER-sample/id/70001456 changes to http://www.example.com/FILTER-sample/id/70001456/

But when i type http://www.example.com/FILTER-sample/id/70001456/rubbishcharacter213342 , the URL does not change to http://www.example.com/FILTER-sample/id/70001456/
The page display right content but the URL is wrong - http://www.example.com/FILTER-sample/id/70001456/rubbishcharacter213342

How do i automatically truncate all 'rubbishcharacter213342' (any character after the last slash) from the URL automatically and change URL to http://www.example.com/FILTER-sample/id/70001456/

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4293624 posted 7:39 am on Apr 8, 2011 (gmt 0)

There are multiple problems with the code.

The $ symbol means "end", so you can't have one of those in the middle of a RegEx pattern.

The (.*) patterns cause your code to attempt tens of thousands "back off and retry" trial matches per URL request. This is very very slow and inefficient. At least the leading (.*) pattern should be changed to something else.

Finally, URLs for folders and for the index page of a folder should end in a trailing slash. The URL for a page should not have a trailing slash and may or may not have an extension.

IF all the URLs to be redirected have /id/ in them, something like this might work.

RewriteRule ^([^/]+/)+/id/([^/]+/)+([^/.]+)+$ http://www.example.com/$1/id/$2/ [R=301,L]

but I would prefer to strip the final slash

RewriteRule ^([^/]+/)+/id/([^/]+/)+([^/.]+)+$ http://www.example.com/$1/id/$2 [R=301,L]

The code recurses multiple folder levels into $1, then matches /id/, then recurses multiple folder levels into $2, and strips the final part if it does not contain a slash or period.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved