Forum Moderators: phranque
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^readnews\.asp$ /readnews/%1? [L]
lots of old urls that consist of example.com/readnews.asp?id=randomnumbers and want to reup these articles to my new contant managment system wordpress using the custom permalinks plugin to add /readnews.asp?id=3453465 as the url however when I do this my server/wordpress doesnt like the special/reserved characters in the url I
WordPress automatically enables the default permalink structure after you install WordPress. The number that is used in the default permalink advises WordPress where the content can be found in your database. To be more specific, the number refers to the ID of the table row in the wp_posts table of your WordPress database (the table prefix for your website will be different if you changed it during the installation process). For example, http://www.yourwebsite.com/?p=50 would refer to the 50th row in your website’s wp_posts table and http://www.yourwebsite.com/?page_id=100 would refer to the 100th. RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^readnews\.asp$ http://example.com/readnews/%1? [L,R=301] [edited by: phranque at 1:35 pm (utc) on Mar 6, 2017]
Still, how will you map the .asp based doc id to the WP-based p=999 id?
I can now just change the wordpress permalink structure in wordpress to readnews
Is there a way that I can via the htaccess use regex to cut out readnews.asp?id= and permanent redirect to the cut out versionDid you mean this kind of thing?
for example
http://example.com/readnews.asp?id=12345678 goes to http://example.com/12345678
RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^readnews\.asp http://example.com/%1 [R=301,L]
RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^readnews\.asp http://example.com/%1 [R=301,L]
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteCond %{QUERY_STRING} id=(\d+)
[0-9]: a number
*: repeat previous
(\d+): numeric, repeated
First one only allows id=numeric, nothing before or after, so is stricter. Second is shorter, and allows anything before or after the id=numeric. Is one better than the other?
depending on the requirements of the application
Is one better than the other?
(^|&)
but this is only needed if you have other parameters whose names end in "id" preceded by a non-word character. Pretty unlikely, right?