Complex htaccess URL and variable rewriting - .htaccess

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]

Related

.htaccess rewrite for orphaned URLs containing underscores and arguments

I only modify the .htaccess with great care for the purposes of my online store.
Some time ago, I did a website migration from osCommerce to OpenCart. This resulted in orphaned osCommerce-style URLs with these two example formats:
http://www.londonpower.com/catalog/product_info.php?products_id=75
http://www.londonpower.com/catalog/product_info.php?cPath=15&products_id=75
Lots of websites in internet-land have links to my old-style URLs, and I have about 100 of them, so I would like to redirect them to new URLs with the following format:
http://www.londonpower.com/2-channel-guitar-preamp
If I understand correctly, the problem has two parts:
to eliminate the underscores, as they baffle the .htaccess engine;
to then perform a 301 redirect on the URL.
So far, I have been able to get the first underscore to change to a hyphen, with this Rewrite Rule:
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
...but no luck with the second underscore (the one that is part of the query string after the "?"). I am stuck there.
I would avoid using rewriting for this. Does the file catalog/product_info.php exist in the new store? If not, create it and add a simple redirection using a map of old IDs to new URLs. If so, do the same thing in a different file, like old-redirector.php then rewrite requests to it.

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

HTAccess Redirect using main parameter, ignore all others

firstly I know and understand how to redirect based on parameters :)
My issue is that I need to redirect all links based on the supplied MenuID parameter and ignore any other information in the query string, as not all parameters are used in each web page request, e.g. menuid=2738421; is New Products
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products& limit=5&page=24
or,
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=20&page=3
or,
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&page=12&limit=15
to
http://www.domain.com/new.html?page=x&limit=x
The reason for the redirection is that search-engines have indexed these pages and so I need to prettify the URLs.
Is this actually possible to create a fuzzy redirect criteria?
## 301 Redirects
# 301 Redirect 1 - works for this explicit URL, but need a partial result
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^new\.html$ http://www.monarchycatering.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24 [R=301,NE,NC,L]
Any help gratefully taken, thank you in advance
Mark.
Sorry for the delay, but StackOverflow doesn't seem to have a way to flag answers that have been replied to and need my attention.
OK, if I understand you correctly, you have an incoming "reduced" semi-SEF URL out in the real world (produced by your links), such as
http://www.domain.com/new.html&limit=5&page=24
("real" SEF would be something like http://www.domain.com/new/limit/5/page/24.html)
and you need to use .htaccess to map it to real files and more Query String information:
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24
You want to detect new.html for example, and replace it by a fixed string shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&, using the [QSA] flag to append the existing Query String on to the end?
RewriteEngine On
RewriteRule ^new\.html /shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products [QSA]
RewriteRule ^sale\.html /shop.php?menuid=32424&menuref=Ja&menutitle=Products+On+Sale [QSA]
...etc...
I believe that a & will be stuck on the end of the rewritten address if the user supplied a non-empty Query String, but be sure to test it both ways.
P.S. It probably would have been cleaner to use "comment" to reply to my question, rather than adding another answer.
It's not clear to me what your starting point is and where you're trying to end up. Do you have "pretty" URLs that you want to convert into "non-pretty" Query Strings that your scripts can actually digest?
The reason for the redirection is that search-engines have indexed
these pages and so I need to prettify the URLs.
If the search engines have already indexed the Query String non-pretty version, they'll have to now re-index with pretty URLs. Ditto for all your customers' bookmarks.
If you want to generate "pretty" links within your site, and decode them (in .htaccess) upon re-entry to non-pretty Query Strings, that should work. Your customers' existing bookmarks should even continue to work, while the search engines will replace the non-pretty with the pretty URLs.
and thanks for the interest in my question...
I have rewritten parts of my website and Google still has references to the old MenuID parameter and shop.php configuration, but now I rewriten the Query to a prettier format, e.g.
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24
is now
http://www.domain.com/new.html&limit=5&page=24
The pages represent product categories, and so needed to be displayed in a more meaningful manner. Customer bookmarking is not an issue, as long as I can redirect the pages.
I hope that makes sense, best wishes,
Mark.

Create search engine friendly urls for our blog

We run a blog, and really need to tidy up the URLs using htaccess, but I am really stumped.
Example:
Working on a site, and I need to generate search engine friendly URLs
So I have the url currently as:
http://mywebsite.com/blog/read.php?art_id=11
Title of this page is:
Why do Australians pay so much for Cars ?
I need to change it to its corresponding SEF url. like so:
http://mywebsite.com/blog/Why-do-Australians-pay-so-much-for-Cars-?
The question mark is part of the title, and we could remove these if its a issue. Any suggestions please?
Also would prefer to drop the read.php portion. Need to create a rule that works across our entire blog.
They all follow the same pattern, only the art_id number changes.
(Assuming that you're using apache as a webserver)
Take a look at this answer for a very similar question: https://stackoverflow.com/a/8030760/851273
The problem here is that .htaccess and mod_rewrite doesn't know how to map page names to art_id's so there's 2 ways you can try to do this.
You can add some functionality to your read.php so that it can do a similar lookup but instead of art_id, it uses art_title or something. Essentially you'll have to do the backend lookup of a database (or wherever your articles are stored) and use the title as a key instead of the ID. This is a little messy since it's possible to have weird characters in titles such as non-ascii or reserved characters (like ? for instance), so you'll need to create a title encoder and decoder when pulling titles out of the database or when using titles to lookup an article in your database.
If you have access to the server config or vhost config, you may be able to setup a RewriteMap using an outside program (the prg type) and create a php script that does the title-to-ID lookup for you. Then you can create rewrite rules in your .htaccess that does something along the lines of:
RewriteRule ^blog/(.*)$ /blog/read.php?art_id=${title-to-id:$1} [L]
Where you are extracting the article title from your pretty URL, and feeding it through a rewrite map called title-to-id to get the art_id. Again you'll need to setup a title encoder/decoder so your titles will have the non-ascci and reserved characters dealt with.
Another thing that you can do is to stick an article ID in your pretty URLs so they look like this: http://mywebsite.com/blog/11-Why-do-Australians-pay-so-much-for-Cars. This is still pretty easy to see what the link is about, it's SEO friendly, and it bypasses the need to do title-to-ID lookups. The Rewrite Rules would also equally be simpler:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here
RewriteRule ^blog/([0-9]+)-(.*)$ /blog/read.php?art_id=$1 [L]
And that's it. Of course, you'd have to now generate all of your blog URL's to be of the form: http://(host)/blog/(art_id)-(art_title), and you'd also have to remove special characters from the title, but you don't have to worry about writing additional code to translate titles back to IDs.

.htaccess ModREwrite

This is a totally new area for me so please be patient. I want to create "permalinks" for a dynamic site I am working on. At the moment all the pages (not the index) are referenced using an ID variable thus:
http://www.domainname.com/page.php?ID=122 (etc)
I want to create a suitable rewrite rule so that a useable URL would be more like this:
http://www.domainname.com/page/'pagetitle'.html (could be .php doen't matter)
Page title is stored in the database and obviously is linked directly to the ID
Am I right in thinking thr rewrite rule would be something like this?
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)ID=([^&]+)(&+(.*))?$
RewriteRule ^page\.php$ /page/%3?%1%5 [L,R=301]
My ideal would be to just create
http://www.domainname.com/'pagetitle'.html
But have absolutly no idea how to do that.
Now the other question/sub question.
If the rewrite works i.e. you type in http://www.domainname.com/page/'pagetitle'.html to a browser address bar does the htaccess file work "the other way" in accessing the page http://www.domainname.com/page.php?ID=122 or do I have to create a function to take the 'pagetitle'.html bit of the URL and convert it to page.php?ID=122 ?
Also, sorry, but this is all new; if I create a site map (xml or php etc) using http://www.domainname.com/page/'pagetitle'.html will the SE spiders go to http://www.domainname.com/page.php?ID=122? or di I need to create the sitemap using the ID variables?
Question 1 and 2:
The condition is not required in this case. Use it like this:
RewriteRule ^/page/([\w-]+).html$ /page.php?title=$1 [L,R=301]
This transforms
/page/blabla.html to /page.php?title=blabla
You need to find the right page using the title parameter in page.php
Question 3:
I suggest you never use the querystring variant of the urls in any of your anchor links or xml sitemap. This way the spiders will only know of the friendly urls.

Resources