: Create redirect from url like www.example.us/?p=100&option 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
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:
example.us/?p=100&option=com_wordpress&Itemid=619
The new URL scheme will exist as:
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 preceding the redirect to URL.
More posts by @Jennifer507
2 Comments
Sorted by latest first Latest Oldest Best
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]
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.