Home > Software engineering >  RewriteEngine Removing too much of my URL
RewriteEngine Removing too much of my URL

Time:06-06

I have studied a lot of different questions here and can't find my exact answer.

I need to remove the center part of a URL that FB is adding but keep the end and beginning. The end will always be different as it's a referral URL.

Example of correct URL:

https://example.com/?p=2&ref=username

Example of how FB is making the URL and it's causing a 404 page not found error:

https://example.com/ERROR-DST-QUERY?p=2&ref=username

Keep in mind that username and p will always be different so I can't hard code what comes after the word username or p.

Here is what I'm currently trying and it's working, meaning it's removing the ERROR-DST-QUERY, however, it's removing everything after the ? in the URL as well.

RewriteEngine On

RewriteCond %{THE_REQUEST} ERROR-DST-QUERY([^\s] ) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(. )$ ERROR-DST-QUERY$1 [L,NC,R=301]

Any help would be appreciated and thank you.

CodePudding user response:

RewriteCond %{THE_REQUEST} ERROR-DST-QUERY([^\s] ) [NC]
RewriteRule ^ /%1? [NC,L,R]

This removes the query string because of the trailing ? on the susbtitution string.

However, the second rule is adding it back again (or at least it is trying to but will fail for the example request you've given) - which doesn't make sense. (And a 301 as well?)

However, the first rule (which is all you need) can be simplified. To redirect https://example.com/ERROR-DST-QUERY?p=2&ref=username to https://example.com/?p=2&ref=username (maintaining the query string, which is entirely variable) you would only need the following:

RewriteRule ^ERROR-DST-QUERY$ / [R=302,L]

The query string is copied by default.

Change the 302 (temporary) redirect to 301 (permanent), if that is the intention, only after you have confirmed this works as intended. You will need to clear your browser cache before testing.

  • Related