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

Apache Web Server Forum

haccess to remove extra characters after slash
haccess to remove extra characters after slash

 5:05 am on Oct 9, 2008 (gmt 0)

I have the following code in my htaccess file that takes the arguement value of "id=$1" and passes it to maincode.php.

RewriteRule (.*)-store/id/([^/]+)$ http://www.example.com/$1-promo-code-discount-coupons-codes/id/$2/ [R=301,L]
RewriteRule store/id/(.*)/ maincode.php?id=$1

The resulting url is : http://www.example.com/theshop-store/id/542125877451369/

In the resulting url above :
1> theshop - is the name of the shop taken from the database
2> 542125877451369 - is the id of the the shop take from the database
3> -store - is just an additional word in the url (static in all urls)

Now the problem is that anything after the "/" also give the same result.
For eg:

How do i redirect these to http://www.example.com/theshop-store/id/542125877451369/

What i want is that whatever happens, when anyone types anything after the last "/" should be redirected to the ending "/".

Please help!

[edited by: jdMorgan at 12:42 pm (utc) on Oct. 9, 2008]
[edit reason] Please use example.com [/edit]



 7:44 am on Oct 9, 2008 (gmt 0)


Maybe changing the '.*' of the second rule?

RewriteRule store/id/([^/]+)/ maincode.php?id=$1

If the id is numeric, you may prefer [0-9]* or even [0-9]{15} if the length is 15 digits.


 8:42 am on Oct 9, 2008 (gmt 0)

If the number is always 15 digits long, then I prefer the
[0-9]{15} because it will stop any and all requests with letters in, and will stop any requests that are too long or too short from even hitting the scripts.

One question. Is the /id/ bit static? If it is, then it does not need to appear in the URL. The fact that there is a 15-digit number there is enough to separate out that it is an id and the rule can key off that, as above.


 12:52 pm on Oct 9, 2008 (gmt 0)

> when anyone types anything after the last "/" should be redirected to the ending "/".

Add something like this before your "store" first rule:

RewriteRule ^([^-]+)-store/id/([0-9]{15})/. http://www.example.com/$1-store/id/$2/ [R=301,L]

If any character (or more than one character) is found after the slash which follows the 15-digit id, then the client is redirected to the same URL, but with those trailing characters removed.

This assumes that no store name will have a hyphen in it. To handle store names with hyphens, you'd need the more-complex and slower-to-process rule:

RewriteRule ^(([^-]+-)+)store/id/([0-9]{15})/. http://www.example.com/$1store/id/$3/ [R=301,L]

However, either will be much faster than using a ".*" subpattern before "store".



 4:08 pm on Oct 9, 2008 (gmt 0)

Thanks for the help but i feel that i have not been very clear with my url details.

Sorry for not being very clear about the store-id and store name. I am sending below the exact version of the script and details of the url variables. Please ignore the earlier ones above.

RewriteRule (.*)-online-store-promotion/store-id/([^/]+)$ http://www.example.com/$1-promo-code-discount-coupons-codes/store-id/$2/ [R=301,L]
RewriteRule online-store-promotion/store-id/(.*)/ promo-code-discount-coupons-codes.php?store-id=$1

Url generated : http://www.example.com/Flowers-online-store-promotion/store-id/542125877451369/

1> The store-id value 542125877451369 here is not fixed. It can be 6 digits or even 15 digits (any value) and its taken from the database.
2> Flowers is the name of one of the stores that has the store-id value 542125877451369 and is also taken from the database.
3> Here the store name is Flowers but it can also be Flow-ers or even Fl123-wrs (can contain hypen)
4> promo-code-discount-coupons-codes.php file takes in the store-id as input.
5> -online-store-promotion is just a made up text in the url that is static for all the urls generated.
6> I cannot change the url format above as its already in the search engines.

My only problem is that when anyone types anything after the last "/" should be redirected to the ending "/".

Please help.

[edited by: jdMorgan at 4:28 pm (utc) on Oct. 9, 2008]
[edit reason] Please use example.com only. [/edit]


 4:26 pm on Oct 9, 2008 (gmt 0)

So, the only discrepancies from the second version of the rule I posted above is that "store" becomes "online-store-promotion" and that your store-id can range from 6 to 15 digits:

RewriteRule ^(([^-]+-)+)online-store-promotion/store-id/([0-9]{6,15})/. http://www.example.com/$1online-store-promotion/store-id/$3/ [R=301,L]
RewriteRule ^([^-]+-)+online-store-promotion/store-id/([0-9]{6,15})/ promo-code-discount-coupons-codes.php?store-id=$2 [L]

A good rule of thumb which I follow here is, never use a ".*" pattern when it can be avoided. Using that ambiguous, promiscuous, and greedy pattern often leads to unexpected matches and always results in rules that are slow to process. This is because ".*" matches nothing, anything, and everything, forcing the pattern-matching engine to try dozens, hundreds, or -- when multiple ".*" subpatterns appear in a pattern -- even tens of thousands of 'trial fits' to find the best match.

You will note that in the Apache URL Rewriting Guide at apache.org, the ".*" pattern is almost never used.

Completely flush your browser cache before testing new code.



 4:47 pm on Oct 9, 2008 (gmt 0)

*** The store-id value 542125877451369 here is not fixed. ***

Shame. Maybe from here on in, you could stick with a fixed format, with a fixed number of digits for all new store IDs?

I am a regular user of fixed-length data wherever and whenever I can - padding with leading zeroes, whatever it takes.


 7:48 am on Oct 10, 2008 (gmt 0)

thanks a lot jim and all others too for the great support.

g1smd : i have learnt from my mistake.....

jim : the code worked like magic. but now the new problem is that when i type the url without the ending slash it does not redirect.
What i mean is as follows :
http://www.example.com/Flowers-online-store-promotion/store-id/542125877451369 - shows no page found, it should actually redirect to http://www.example.com/Flowers-online-store-promotion/store-id/542125877451369/

> your store-id can range from 6 to 15 digits - its actaully not fixed to any number, the range can be from 1 to just about anything, still i have changed the range to {2,30} for safety, is there a better way ?

thanks a ton.


 1:20 pm on Oct 10, 2008 (gmt 0)

Fix missing trailing slash as well as extra appended junk:

RewriteRule ^(([^-]+-)+)online-store-promotion/store-id/([0-9]{2,30})[b](/.+)?$[/b] http://www.example.com/$1online-store-promotion/store-id/$3/ [R=301,L]


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