How to create a rewriterule with multiple query strings? - string

I have a few URL's from an old forum I wish to redirect to the same content on another forum. Here is a (fictional) example of an old URL: http://www.oldforum.com/forum/index.php?topic=21.0;msg=100
It should redirect to: http://www.newforum.com/threads/topic.11
Old and new URL's do not have anything in common, so I will be writing manual rewrites for each. I tried a number of things, but I can't get it to work. Here's what I have:
RewriteCond %{QUERY_STRING} ^topic=21.0;msg=100$
RewriteRule ^oldforum/index\.php$ http://www.newforum.com/threads/topic.11? [R=301,L]
I'm close, right?

Correct me if I'm wrong:
The user in his/her browser types:
http://www.oldforum.com/forum/index.php?topic=21.0;msg=100
and the URL, on the server side, is redirected to:
http://www.newforum.com/threads/topic.14
You have to look into your query string to be able to do a comparison.
You've got the only solution that comes in my mind here:
Using rewritemap for query string
From there you'll be able to do your own "correspondance table" or sort of.
Hope this helps.

Related

htaccess - get content on one url from another url

Is there the opportunity to get content on one url from another url? like this:
test.com/dir1 shows content from query test.com/getinfo/index.php?q=dir1
or
test.com/dir2 shows content from query test.com/getinfo.php?q=dir2
I need that because i must do 50 urls like test.com/dir1 ../dir50 which has the same template but with some different content based on query and one virtual url with query is better than 50 real directories.
I tried do but couldnt! Thanks in advance!
If it is all on the same host, then have a look at mod_rewrite, which can do amazing things. Just make sure you read the manual through, as there are a lot of details to take in.
RewriteEngine On
RewriteRule ^/dir1$ /getinfo/index.php?q=dir1 [L]
or more generically:
RewriteEngine On
RewriteRule ^/dir(.*)$ /getinfo/index.php?q=dir$1 [L]

301 redirect with query strings

I'm new to programming and brand new to this site (although it has helped out many times as I'm trying to learn this stuff...so THANKS for all the help so far!).
My question relates to 301 redirects. I've been searching this site as well as many other pages through google and can't seem to find a solution that works for me (I'm guessing that the solution is probably already out there since it seems like a common problem...but I haven't yet been able to find it).
So here it is:
I have a site where: http://homework-heroes.com/php/views/newAssignment.php?[then any query string] always goes to the same page.
For the sake of eliminating duplicate content as seen by google, I want these to always redirect to: http://homework-heroes.com/php/views/newAssignment.php?final
What is the htaccess code I should insert to accomplish this?
Thanks in advance!
Place the following in your /.htaccess file:
RewriteRngine On
RewriteRule %{QUERY_STRING} !^$
RewriteRule %{QUERY_STRING} !^final$
RewriteRule ^(php/views/newAssignment.php)$ /$1?final [R=302,NC,L]
The above basically says that if the query string is not blank and a request is being made to /php/views/newAssignment.php, then redirect to the same page with ?final as the new query string.
Alternatively, if you would like to remove the query string altogether, just remove final,leave the question mark in the rule, and remove the second condition.
If you're happy and want to make the redirect permanent, change 302 to 301.

Complex htaccess URL and variable rewriting

I run a mmo game fan site http://www.ddmsrealm.com. In this site I have run a databse with quest and item information for years. It was originally built with MSSQL/ASP and I have recently converted it to MYSQL/PHP. In this conversion I optimized the database. In doing so I changed a bit of the structure and variables. Now, the new pages are up and running but I am having a terrible time trying to write rules in my htaccess file to accommodate the hundreds of quests and thousands of items to reroute to the new database pages with the new database variables. Any and all help would be appreciated as I struggle to learn apache/htaccess.
I have two “details” pages that need to be rerouted along with having the variable names changed. The actual ID's are still the same so it should work.
Original Quest Page was an asp page:
~http://www.ddmsrealm.com/ddo/quests/QDetail.aspx
Permanent reroute to new Quest Details Page(WordPress Template Page):
~http://www.ddmsrealm.com/index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info
Now the parameters need to be rewritten like this:
Old: QuestID to New: ddoQuestID
Old: SeriesID to New: ddoSeriesID
So the idea is that this:
~http://www.ddmsrealm.com/ddo/quests/QDetail.aspx?QuestID=210&SeriesID=30
Is permanently redirected as this:
~http://www.ddmsrealm.com/index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info?ddoQuestID=210&ddoSeriesID=30
If I can get the formula and steps for this I am sure I can apply it to the other pages. Being new to htaccess rewriting I am struggling to understand what exactly needs to happen and in what order. As you can imagine, this has jacked up my SEO bad having about 5000+ pages now going to a 404. Plus hundreds of websites have linked that old URL and now those links are trashed until I can resolve this.
Thank you so much in advance for the help figuring this out!
The URI rewrite itself is pretty straight forward using a RewriteRule but changing the query string you'll need to use a RewriteCond and match against the %{QUERY_STRING} variable and backreference the matches using the % symbol. See the docs for more info: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Try something like this:
RewriteEngine On
# Match the query string
RewriteCond %{QUERY_STRING} QuestID=([0-9]+)&SeriesID=([0-9]+)
# Rewrite the URI and append the new query string
RewriteRule ^ddo/quests/QDetail.aspx$ /index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info?ddoQuestID=%1&ddoSeriesID=%2 [R=301,L]

htaccess to rewrite param=NUMBER into /text

I am trying to figure out how to rewrite URLs from something like this:
example.com/collection.php?collection=1
Into:
example.com/collection-name.php
I recently redesigned an e-commerce site and need to redirect the old URLs to the new ones. I've seen loads of instructions on how to use the value of collection=1 in a rewritten URL but not how to use text instead of the value. There are only 5 collection values that need to be redirected but in addition there are also old URLs that have multiple params in them that also need to be rewritten/redirected as text. I hop that made sense.
So I think I can get them all worked out if I can get the initial redirect set up.
Other/more complex old URLs look like this:
example.com/collection.php?collection=1&product=2&item=3
Which would then need to be redirected to:
example.com/collection-name/sub-collection-name/product-name.php
Not sure exactly how to go about doing this. I don't want to write line after line in the .htaccess file but I have no idea of how else to accomplish this.
Thanks in advance, I appreciate andy and all help in this matter!
EDIT------------------------------------
Here's my new rewrite condition and rule based on the info provided to me. This would be the rule for the URLs containing all the query params using 3 separate RewriteMaps.
RewriteCond %{QUERY_STRING} collection=([^&]+)&product=([^&]+)&item=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}/${rangemap:%2}/${productmap:%3}\.php [R=301,L]
Please let me know if anything looks off or I missed anything. Wasn;t sure if I needed to use $1, $2, $3 for each of the query params. I'm still a NOOB with rewrites. Thanks!
Edit------------------------------------------------------
Using the following code in my .htaccess file:
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}\.php [R=301,L]
My URLs that start as "example.com/collection.php?collection=1
Are being rewritten as: example.com/shop/.php?collection=1
I am a bit lost on this one. Could it be that my Redirect Maps are not being read? Or is it something else? Thanks for the help.
You want to look into using a RewriteMap. You can specify the maps you want for collection, sub-collection and products, and then look up the text for each number.
So you would define a RewriteMap in your virtualhost config with something like
RewriteMap categmap txt:/path/to/category/map.txt
Then your rewrite rule would look something like
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php ${categmap:%1} [L,R]
Add more RewriteConds for your more complicated cases and make separate rules out of them. Place the more specific rules first in the file, then the more general ones.

URL Rewriting based on form input

I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:
site.com/go/?id=whateverIType
and I want to change it to
site.com/go/whateverIType
I was thinking on Mod Rewrite, but if the user put something in the URL, like:
site.com/go/?dontwant=this&id=whateverIType&somemore=trash
I want to ignore the other variables but "id", and rewrite the rule.
What's the better way of get this done? Thanks in advance!
PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".
I'm not familiar with CodeIgniter, but you can try the following RewriteRule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.
Hope this helps.
Mod_rewrite supports conditions and rules with RegEx, so you could have a rule that matched the ?id=XXXX, that would extract it from the URL (keeping the other parameters), and rewrote the URL accordingly.
However... I don't think you want to do this, because if you rewrite the URL to be /go/Some+Search+Query, you won't be able to pick it up with say, PHP, without parsing the URL out manually.
It's really tough to have custom, SEO-friendly URLs with user input, but it is technically possible. You're better off leaving in the ?id=XXX part, and instead, using mod_rewrite in the opposite approach... take all URLs that match the pattern /go/My+Search+Terms and translate that back into something like ?id=My+Search+Terms, that way you'll be able to easily parse out the value using the URL's GET parameters. This isn't an uncommon practice - Google actually still uses URL parameters for user input (example URL: http://www.google.com/search?q=test).
Just keep in mind that mod_rewrite rewrites the URL before anything else (even PHP), so anything you do to the URL you need to handle. Think of mod_rewrite as a regular expression-based, global "Find and Replace" for URLs, every time a page is called on the server. For example, if you remove the query string, you need to make sure your website/application/whatever accounts for that.
In application/config/routes.php
$route['go/(:any)'] = "go/index/$1";
Where go is your controller and index is the index action.
http://codeigniter.com/user_guide/general/routing.html
You can use something like this in your .htaccess if you aren't already:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources