Forum Moderators: phranque

Message Too Old, No Replies

.htaccess change from ?osCsid.html to .html

Remove query string from url

         

sultanos

1:03 am on Jan 31, 2009 (gmt 0)

10+ Year Member



Hy i was wondering if someone could please help me with this.
My oscommerce gives product links as
"my-product-p-80?osCsid.html"
I would like this to be CHANGE TO
"my-product-p-80.html"

I have tryed with this but seems i am doing something wrong (...and many others as my imagination told me , obviusly i dont know how to use this...)
Rewriterule ^(.*)?osCsid.html$ \.html

i want just to remove the "?osCsid" but only when is beside the ".html"
FROM this "?osCsid.html" to ".html" , keeping what is before the "?osCsid.html"

Thanks in advance

jdMorgan

1:30 am on Jan 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Our Apache Forum Charter [webmasterworld.com] contains links to several useful resources. I strongly recommend reviewing the Apache mod_rewrite documentation, and studying the RewriteCond and RewriteRule directives in detail.

Our Apache Forum Library [webmasterworld.com] also contains several useful tutorials and threads. You may find this one [webmasterworld.com] to be particularly useful in answering your question.

You can find the links to Forum Charters and the Library at the top of almost every page on WebmasterWorld.

Jim

sultanos

11:04 am on Jan 31, 2009 (gmt 0)

10+ Year Member



Thanks morgan . I read that before posting but i am lost . Can this be done?if so could someone write how?

jdMorgan

2:57 pm on Jan 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The short answer is that you must modify your script (or "wrap it" inside another script) if you really want to change the URL. URLs are defined on the Web, and not by the server.

Once the new URLs appear on your pages, you can use mod_rewrite to tell the server where to find the content to be delivered when those new URLs are requested. See this tutorial [webmasterworld.com] in our library.

Due to the limited number of contributors (helpers) here and the high level of demand, we cannot write your code for you; However, we will be happy to help you get your own code working once you have made a good effort, as explained in our Charter.

Thanks,
Jim

g1smd

6:22 pm on Jan 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Change the links ON your pages to use the new URL format. Links 'define' URLs.

Craft a new RewriteRule to connect those URL requests with the real internal server filepath.

sultanos

12:50 am on Feb 1, 2009 (gmt 0)

10+ Year Member



I am almost there i am getting from this:
http://www.example.com/malvasia-brut-nature-p-90?osCsid.html
TO THIS
http://www.example.com/malvasia-brut-nature-p-90?.html
I just need to get rid off the "?"
i am using this
RewriteCond %{QUERY_STRING} ^(.+)?osCsid.html$ [NC]
RewriteRule (.*) $1?%1.html [R=301,L]

[edited by: jdMorgan at 1:31 am (utc) on Feb. 1, 2009]
[edit reason] Example.com [/edit]

g1smd

1:49 am on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A question mark appended to the end of the target URL in the rule will do what you want.

jdMorgan

2:17 am on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We need to know what the query string looks like if there is another parameter in front of osCsid.html

Jim

sultanos

11:15 am on Feb 1, 2009 (gmt 0)

10+ Year Member



http://www.example.com/product_info.php?products_id=90?osCsid=be3c95ee4a1cc5bb1ac878589f323758

jdMorgan

6:44 pm on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That URL is not consistent with your stated requirements:

http://www.example.com/product_info.php?products_id=90?osCsid=be3c95ee4a1cc5bb1ac878589f323758

"my-product-p-80?osCsid.html"
I would like this to be CHANGE TO
"my-product-p-80.html"

One URL has "my_product-p-80.html" and the other has "product_info.php", so this is confusing.

Jim

sultanos

8:33 pm on Feb 1, 2009 (gmt 0)

10+ Year Member



my links show it that way with the oscsid normal dont show them , i probably mess up the hole thing , the problem is that this same config works at another host with no problem.
This is what should be
product_info.php?products_id=80
And here is my .htaccess
# Ultimate SEO URLs BEGIN
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
AcceptPathInfo On
php_value session.use_trans_sid 0
php_value register_globals 1

RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^options\=(.*)$
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2%1
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-m-(.*).html$ index.php?manufacturers_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pi-(.*).html$ popup_image.php?pID=$2&%{QUERY_STRING}
RewriteRule ^(.*)-t-(.*).html$ articles.php?tPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-a-(.*).html$ article_info.php?articles_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pr-(.*).html$ product_reviews.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pri-(.*).html$ product_reviews_info.php?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-i-(.*).html$ information.php?info_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-links-(.*).html$ links.php?lPath=$2&%{QUERY_STRING}
# Added polls and newsdesk
#RewriteRule ^(.*)-po-([0-9]+).html$ pollbooth.php?pollid=$2&%{QUERY_STRING}
RewriteRule ^(.*)-n-(.*).html$ newsdesk_info.php?newsdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-nc-(.*).html$ newsdesk_index.php?newsPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-nri-(.*).html$ newsdesk_reviews_info.php?newsdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-nra-(.*).html$ newsdesk_reviews_article.php?newsdesk_id=$2&%{QUERY_STRING}
# BOF: Faqdesk support added by faaliyet
RewriteRule ^(.*)-f-(.*).html$ faqdesk_info.php?faqdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-fc-(.*).html$ faqdesk_index.php?faqPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-fri-(.*).html$ faqdesk_reviews_info.php?faqdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-fra-(.*).html$ faqdesk_reviews_article.php?faqdesk_id=$2&%{QUERY_STRING}
# EOF: Faqdesk support added by faaliyet
# RewriteCond %{QUERY_STRING} ^(.+)?osCsid.html$ [NC]
# RewriteRule (.*) $1?%1.html [R=301,L]

# Ultimate SEO URLs END

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /error404.html

g1smd

9:33 pm on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



All those
(.*)
parts in the patterns are very inefficient, meaning that dozens or hundreds of trial matches have to be made for each URL for each and every rule.

Most of those should be replaced by something like

([^\-]+)
and
([^.]+)
just as long as the part to be captured does not contain any hyphens or periods.

Additionally, every rule needs an

[L]
appended to the end, to stop processing at that point.

[edited by: g1smd at 9:56 pm (utc) on Feb. 1, 2009]

jdMorgan

9:53 pm on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In order to solve these problems, you must know --and state here for us-- *what* is the requested URL, and *what* is the desired redirect URL or rewrite filepath. So unfortunately, nothing posted above answers my question.

All I can tell you is that you will have problems if the "products_id" query name/value is missing with this rule:


# RewriteCond %{QUERY_STRING} ^(.+)?osCsid.html$ [NC]
# RewriteRule (.*) $1?%1.html [R=301,L]

Most likely, you need another rule to handle the missing products_id case, and you need to modify the existing rule:

# Redirect requested URL [b]with[/b] "products_id" in query string
RewriteCond %{QUERY_STRING} ^[b]([^?&]+)[?&]os[/b]Csid\.html$ [NC]
RewriteRule (.*) http://www.example.com/$1?%1.html [R=301,L]
#
# Redirect requested URL [b]without[/b] "products_id" in query string
RewriteCond %{QUERY_STRING} ^osCsid\.html$ [NC]
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

-----

Now, I'm going to suggest some improvements to your existing rules, because most of them are very inefficient, and some of them should not even be needed. Making your regular-expressions patterns *much* more specific will likely improve your server performance noticeably. Even if it doesn't, it will help to keep you from having to upgrade your server so soon if your site gets very successful/busy:

Again, most of this is just making your patterns more specific. We're also adding the [L] flag except where we *know* it should not be used, escaping all literal periods in regex patterns, and getting rid of the un-referenced back-references.


RewriteCond %{HTTP_HOST} ^example\.com [OR]
# catch FQDN and/or appended port number
RewriteCond %{HTTP_HOST} ^www\.example\.com(\.¦\.?:[0-9]+)$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{QUERY_STRING} ^options=(.*)$
RewriteRule ^[^\-]+-p-([^.]+)\.html$ product_info.php?products_id=$1%1 [L]
RewriteRule ^[^\-]+-p-([^.]+)\.html$ product_info.php?products_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-c-([^.]+)\.html$ index.php?cPath=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-m-([^.]+)\.html$ index.php?manufacturers_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-pi-([^.]+)\.html$ popup_image.php?pID=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-t-([^.]+)\.html$ articles.php?tPath=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-a-([^.]+)\.html$ article_info.php?articles_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-pr-([^.]+)\.html$ product_reviews.php?products_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-pri-([^.]+)\.html$ product_reviews_info.php?products_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-i-([^.]+)\.html$ information.php?info_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-links-([^.]+)\.html$ links.php?lPath=$1&%{QUERY_STRING} [L]
# Added polls and newsdesk
#RewriteRule ^[^\-]+-po-([0-9]+)\.html$ pollbooth.php?pollid=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-n-([^.]+)\.html$ newsdesk_info.php?newsdesk_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-nc-([^.]+)\.html$ newsdesk_index.php?newsPath=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-nri-([^.]+)\.html$ newsdesk_reviews_info.php?newsdesk_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-nra-([^.]+)\.html$ newsdesk_reviews_article.php?newsdesk_id=$1&%{QUERY_STRING} [L]
# BOF: Faqdesk support added by faaliyet
RewriteRule ^[^\-]+-f-([^.]+)\.html$ faqdesk_info.php?faqdesk_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-fc-([^.]+)\.html$ faqdesk_index.php?faqPath=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-fri-([^.]+)\.html$ faqdesk_reviews_info.php?faqdesk_id=$1&%{QUERY_STRING} [L]
RewriteRule ^[^\-]+-fra-([^.]+)\.html$ faqdesk_reviews_article.php?faqdesk_id=$1&%{QUERY_STRING} [L]
# EOF: Faqdesk support added by faaliyet
#
# Redirect requested URL with "products_id" in query string
RewriteCond %{QUERY_STRING} ^([^?&]+)[?&]osCsid\.html$ [NC]
RewriteRule (.*) http://www.example.com/$1?%1.html [R=301,L]
#
# Redirect requested URL without "products_id" in query string
RewriteCond %{QUERY_STRING} ^osCsid\.html$ [NC]
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
#
# Ultimate SEO URLs END
#
# Do not do this! Apache will return a 200-OK status, confusing search
# engine spiders, and potentially damaging your search rankings!
# Let Apache handle missing-resource errors normally, instead.
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule (.*) /error404.html

The changes to the RewriteRule patterns assume that the requested URL format is, for example,
"/<single_word>-<keyletter_or_letters>-<desk-id>.html"
If more than one word can appear in the first part of the URL shown here as "<single_word>", then change the first part of the patterns from "[^\-]+" to "(\-.]+)*", and change the back-references back from $1 to $2. Don't do this unless you have to though, as you will lose a good part of the efficiency gain we've just won.

Based on the number of inefficiencies found and corrected in the code, I think you will probably notice a speed-up on your server with these changes...

Jim