htaccess - get content on one url from another url - .htaccess

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]

Related

.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]

RewriteRule from download.php?token=something into /download/something

I am working on url, I try htaccess, php, javascript and many other things but unable to figure it out.
My url is :
example/cheap-flight-to.php?country=lagos
and I want to change the url something like this :
example/cheap-flight-to.lagos
or
example/cheap-flight-to/lagos
please help me
The following should allow you to generate your urls in the format that you wish.
RewriteEngine On
RewriteBase /
RewriteRule ^example/cheap-flight-to/([a-zA-Z]+)$ /example/cheap-flight-to.php?country=$1 [NC,L]
What you want could be done using regular expressions in .htaccess, but it makes no sence, since it wouldn't be pointing to anything, unless you have a directory cheap-flight-to/lago in which you have an index.php that will show-up in the browser or return data. This means you have to setup a directory for each destination you want to go to. Is that really what you want? Usually it's been used the otherway around. The user (or a script for that matter) enters a url like "example/cheap-flight-to/lagos". Then through regular expressions in .htaccess you rewrite the url to "example/cheap-flight-to.php?country=lagos".
Have a look at http://httpd.apache.org/docs/1.3/misc/rewriteguide.html and
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for more on rewriting rules in .htaccess.

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]

How to create a rewriterule with multiple query strings?

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.

.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