Remove a Specific String from Url and Redirect

Redirect rule to remove query string from URL

To remove the query string you first need to check that there is a query string to remove, otherwise, it should do nothing.

For example, to remove the query string from /blog/?<query-string> you would do something like this:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(blog)/?$ /$1/ [QSD,R=302,L]

This matches the URL-path blog/ (trailing slash optional) and redirects to /blog/ (with a trailing slash). Your example URL includes the trailing slash, but your regex appears to suggest the trailing slash is optional?

The preceding condition (RewriteCond directive) checks the QUERY_STRING server variable to make sure this is non-empty (ie. it matches a single character, denoted by the dot).

The $1 backreference in the substitution string contains the value from the captured group in the preceding RewriteRule pattern. ie. "blog" in this example. This simply saves repetition. You could just as easily write RewriteRule ^blog/?$ /blog/ [QSD,R,L] instead.

The QSD (Query String Discard) flag removes the original query string from the redirected response, otherwise, this would be passed through by default (which would create a redirect-loop).

If the request does not contain a query string then this rule does nothing (since the condition will fail).

If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent), but only once you have confirmed this works as intended. 301s are cached persistently by the browser so can make testing problematic.


A look at your existing rules:

was trying RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.

By default, the relative substitution string (ie. blog/) is seen as relative to the directory that contains the .htaccess file and this "directory-prefix" is then prefixed back to the relative URL, so this will (by default) result in a malformed redirect of the form https://example.com/path/to/public_html/blog/.

Also tried RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got message that page is not working, 'url' redirected you too many times.

This is not checking for (or removing) the query string so this is basically just redirecting to itself - an endless redirect-loop.



Remove any query string from any URL

What rule do i write, to remove query string from any URL.

Modify the RewriteRule pattern to match any URL and redirect to the same. For example:

RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /$1 [QSD,R=302,L]

This needs to go at the top of the root .htaccess file before any existing rewrites.

If the .htaccess file is in a subdirectory (not the root) then you will need to do something like the following instead, since the $1 backreference (as used above) won't contain the complete root-relative URL-path.

RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=302,L]

Remove a string from an URL and redirect to it

This probably is what you are looking for:

RewriteEngine on
RewriteRule ^/?document/this-(.+)$ /document/$1 [L]

If you want to redirect the client externally, then alter it slightly:

RewriteEngine on
RewriteRule ^/?document/this-(.+)$ /document/$1 [R=301]

Remove query string from redirected URL with htaccess

You were close to the answer... You have the ? on the wrong side. Put it on the redirect side to strip off the query string:

RewriteRule ^viewtopic.php http://www.myurl.org.uk/? [L,R=301]

In a 301 redirect, mod_rewrite will normally append the full query string. But placing a ? at the end of your rewritten URL without a corresponding [QSA] ("querystring append") flag will instruct it instead to use the blank query string you supplied.

how do I redirect a url and remove part of the string

Providing you don't have any existing mod_rewrite directives then you can do something like the following using a mod_alias RedirectMatch directive near the top of your .htaccess file:

RedirectMatch ^/thispart/([\w-]+)-remove$ /newpart/$1

Note that this removes "-remove", as in your example, not simply the string "remove".

The path segment before the "-remove" part is assumed to consist of the characters 0-9, a-z, A-Z, _ or -.

This is a temporary (302) redirect.

You will need to clear your browser cache before testing.


However, if you have existing mod_rewrite directives then you should use mod_rewrite instead to avoid potential conflicts (and improve efficiency). For example:

RewriteRule ^thispart/([\w-]+)-remove$ /newpart/$1 [R=302,L]

(Note the absence of the slash prefix on the RewriteRule pattern.)

htaccess remove ? from the url and redirect

You may use this rule:

RewriteCond %{QUERY_STRING} .
RewriteRule ^jobs/job-(\d+\.html?)$ %{REQUEST_URI}? [NC,L,R=301]

? after $1 in target will remove any query string.

htaccess remove query string on redirected url

Try this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} ((?:^|&)id=([^&]+)|(?:^|&)mk=([^&]+))
RewriteRule ^view\.php$ view.php? [L]

The first part checks to make sure a querystring exists to avoid redirect loops.

The second part checks to see if an id= or mk= parameter exists in the querystring.

The third part then redirects view.php (with or without a querystring) to the same file but with the querystring trimmed off. This might still show a useless question mark in the URL in which case, see if QSD (querystring discard) is any better. Note the L flag for last rule is used. This will prevent any further rewrites for the current iteration. Apache still needs to scan the file from the top again to make sure it doesn't need to rewrite further.

If this is going to encounter a redirect loop from other rewrite rules you're not showing us, might need to utilize a trick like the following at the top of your .htaccess file:

RewriteEngine On

#Break infinite loops
RewriteCond %{ENV:REDIRECT_SKIP_ROOT_HTACCESS_FILE} =1
RewriteRule ^.*$ - [L]


# ...
# more logic can go here
# ...

# trim querystring from bad links
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} ((?:^|&)id=([^&]+)|(?:^|&)mk=([^&]+))
RewriteRule ^view\.php$ view.php? [L,ENV=SKIP_ROOT_HTACCESS_FILE=1]

If you need to test for mk=[1-9][0-9]{0,1} or id=[1-9][0-9]{0,3}, the above should be easy enough to edit. Simply replace the instances of ([^&]+) with your numeric logic.



Related Topics



Leave a reply



Submit