Create redirect from url like www.example.us/?p=100&option=

Create redirect from url like www.example.us/?p=100&option= - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the Create redirect from url like www.example.us/?p=100&option= error on your web browser. Problem :


I am switching CMS and have about 600 URLs that need to get redirected to the new scheme. The URL on the old site looks like the following on my test server:



www.example.us/?p=100&option=com_wordpress&Itemid=619


The new URL scheme will exist as:



www.example.us/blog.php?p=100


Some old URLs use p=, others id= or catid=. I have tried various redirects like the following, all failing for various reasons:



RewriteEngine on
RewriteBase /

RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d

RewriteCond %REQUEST_URI ^/$
RewriteCond %QUERY_STRING ^p=100
RewriteRule .* 100.php


The result is that it redirect to 100.php but appends the query string to the url and ends up like:



example.us/100.php?p=100&option=com_wordpress&Itemid=619


That is the closest to a solution. I have also tried the following with the failure message noted below:



RewriteCond %REQUEST_URI  ^/$
RewriteCond %QUERY_STRING ^p=100
RewriteRule ^(.*)$ 100.php
# Fails as it appends query string

RewriteCond %REQUEST_URI ^/$
RewriteCond %QUERY_STRING ^p=100
RewriteRule .* 100.php$
# Fails with The requested URL /100.php$ was not found on this server.

RewriteEngine on
RewriteCond %QUERY_STRING ^p=100$
RewriteRule ^index.php$ 100.php [L,R=301]
# Fails redirecting to /index.html

RewriteRule p=100 100.php [R=301,L]
# Fails redirecting to /index.html

RewriteRule ^p=100$ 100.php [R=301,L]
# Fails redirecting to /index.html


I tried many of the above with/without http:// preceding the redirect to URL.


Solution :

This assumes that p, id or catid always appears at the start of the query string, and that the value of this parameter is the "file" basename in the new URL, as per your code examples.



RewriteEngine On
RewriteCond %QUERY_STRING ^(p|id|catid)=(d+)
RewriteRule ^$ /%2.php? [R=301,L]


The ^$ pattern only processes requests for the document root (ie. example.com/). The %2 back reference refers to the 2nd parenthesised sub pattern in the CondPattern (ie. (d+)). If you omit the R flag (and don't specify an absolute URL) then it will result in an internal rewrite, not an external redirect as I would assume is required here (ie. the URL in the address bar would not change).



The ? on the end of the RewriteRule substitution strips the original query string from the rewritten URL. This essentially creates an "empty" query string (the ? is not present in the result). Alternatively you can use the QSD (Query String Discard) flag on Apache 2.4+



By default, unless you specify a new query string on the RewriteRule substitution then the original query string will be copied onto the rewritten URL (as you have found). If you ever wanted to merge the original query string with a new one that you specify then you would need to use the QSA (Query String Append) flag.



I figured out how to solve the second part of the question where the ID= was found at the end like the following:



example.com/index.php/archived-content?id=206


by adding a line to the .htaccess like:



RewriteCond %QUERY_STRING ^id=206
RewriteRule (.*) /some-file-name.php? [R=301,L]

We hope that this article has helped you resolve the htaccess, redirects, error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

How do I redirect traffic only if being accessed from a specific port?