htaccess remove parameter from querystring

Posted on 2011-05-02
Last Modified: 2012-05-11
I am trying to find a simple .htaccess snippet to rewrite URLs to remove a parameter from the URL.

The text that needs to be removed is ?fb_xd_fragment=   or &fb_xd_fragment=   which is usually found at the end of the querystring.

The rest of the url and query string (if there is one) needs to be unchanged.

[ NB - This is occasionally inserted at the end of urls due to a well documented bug in Facebook like/ comments box (NB - I am aware of the workaround to this issue that Facebook recommend- please do not suggest that. I am specifically looking for an .htaccess rewrite solution here). ]

Thanks for your help.
Question by:dannyddd

    Accepted Solution

    OK, I think I've answered my own question:

    This appears to work:
    RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)fb_xd_fragment=(&+(.*))?$
    RewriteRule (.*)?$ %{REQUEST_URI}?%1%4 [L,R=301]

    I won't close the question just yet -

    If anyone can spot any issues with this, or ways it can be optimised, please let me know.
    LVL 4

    Assisted Solution

    RewriteEngine On
    # remove one query parameter and keep the others
    RewriteRule ^(.*/\?)(.*)fb_xd_fragment=[^\&]*\&(.*)$ $1$2$3 [L]
    # remove all query string if there are no other query parameters
    RewriteRule ^(.*/)\?fb_xd_fragment=[^\&]*$ $1 [L]

    Author Comment


    Thanks - what is the benefit of your solution versus the one I already have?
    NB - the one I have seems to work, so far.
    LVL 4

    Assisted Solution

    You probably should keep yours because i didn't test mine at all. But your solution does that via redirect, i e client receives 301 redirect and makes second request.

    If latency is not an issue then this is not a big deal.

    Author Closing Comment

    My own solution to this seems to be working well. But thanks a1j for your useful help and advice.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    TCP/IP Socket - connection close results in data lost 14 98
    mysql_connect PHP 7 13 412
    XAMPP / PHPmyadmin 5 58
    php and mysqli 15 28
    If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
    If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now