PDA

View Full Version : .htaccess mod_rewrite help


destrugter
09-28-2009, 01:43 AM
Hello everyone. I've recently learned that mod_rewrite can help me make some cool urls into more memorable ones for users making it easier. So I searched the forums and found out that .htaccess was finally allowed on paid accounts here at ulmb. So I made my .htaccess file in my main directory.

I am having a little difficulty with it. I have made the file .htaccess and it has the following contained in the file.


Options +FollowSymLinks
RewriteEngine on
RewriteRule items/view/(.*)/$ /items/view_item.php?Items=$1


If I'm correct, this should turn my page from "/items/view_item.php?Item=$1" to "/items/view/$1.html"

Sadly, when I try this out in Opera and IE, it does not rewrite my url. Could someone help me with what I'm doing wrong? As far as I can see, it should work.

UnlimitedMB
09-28-2009, 01:48 AM
The server does not allow you to change the "Options", and that line is unnecessary for what your trying to do.
Using not allowed functions will make the entire .htaccess result in server error.

This page explains how to use mod_rewrite.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

destrugter
09-28-2009, 01:14 PM
Thanks, that site was very helpful. It's working like a charm now :)

EDIT:
Ok, so I've been messing around with this to do some cool things. I would like to know how to make it so the url reads: runenerds.com/items/ITEM_NAME

So, the closest I've gotten to that is getting it to work, but the rest of the scripts (updating the items and such) won't work. The closest I've gotten it to work with the scripts working is: runenerds.com/items/view.ITEM NAME

My code for the .htaccesse is

RewriteEngine on

RewriteBase /items/
RewriteRule ^view\.(.*)$ view_item.php?Item=$1 [QSA]


Any help would be appreciated.

destrugter
09-29-2009, 09:10 PM
I've tried


RewriteEngine on

RewriteBase /items/
RewriteRule ^(.*)$ view_item.php?Item=$1 [QSA]


but it won't process my mysql inserts.

Tyler
09-29-2009, 09:51 PM
Is it trying to pull the variable Item or item?

destrugter
09-29-2009, 10:11 PM
Well my script when it was using


RewriteEngine on

RewriteBase /items/
RewriteRule ^view\.(.*)$ view_item.php?Item=$1 [QSA]


works perfect. But when I use the one I provided you, none of the mysql statements work. In fact, when you click submit, it clearly shows the page and the item name, but does not process it. The page remains the same, but the url changes.

Tyler
09-30-2009, 12:48 AM
What page is the form submitting to? Do you have a link to this page that you can post on here?

destrugter
09-30-2009, 02:12 AM
The page has options where you can edit the item's information in the database. When you click submit, each one has it's different form (probably not as organized as you can get it, but I'll fix that once I get the rest of this sorted). So let's say you were updating the general information of the item. When you click submit, it links you to http://runenerds.com/items/update_general.php?Item=ITEM NAME

Now, it will go there, and change the url to that, but it doesn't change the page at all. It's supposed to display a thank you message and such. Just to test if it updates the information, I let it sit for about 5 minutes and went to the item's page and nothing had changed.

Tyler
09-30-2009, 05:30 AM
It sent me to http://runenerds.com/items/view.ITEM after a little while. I am not sure what your trying to do.

destrugter
09-30-2009, 02:50 PM
Ok, I'll try to be more clear. I wanted to use .htaccess' mod_rewrite to make the url's cooler and easier to remember for the users. So right now, my code is


RewriteEngine on

RewriteBase /items/
RewriteRule ^view\.(.*)$ view_item.php?Item=$1 [QSA]


Ok, so lets say the user is wanting to look up the item gold bar. He would type in the url box, runenerds.com/items/view.gold bar

Now, that is a lot easier to remember than runenerdes.com/items/view_item.php?Item=gold bar for more users. Now when you go to the newer url (view.gold bar) and edit the information, then submit, it works correctly. It processes it, displays a thank you, and takes you back to page after 10 seconds.

Ok, I wanted to go a step further and make it so the user only has to type in runenerds.com/items/gold bar

That's a lot easier to remember! But when I change the code to


RewriteEngine on

RewriteBase /items/
RewriteRule ^(.*)$ view_item.php?Item=$1 [QSA]


if I'm reading correctly, that's supposed to do what I want. Make the url like that. And it does! But the only problem is when you edit an item it won't process the edited information.

UnlimitedMB
09-30-2009, 03:25 PM
Does your script view_item.php expect to be used in this way?

You cant just use mod_rewrite on a script that doesn't expect mod_rewrite input, because it has to handle it differently.

destrugter
09-30-2009, 03:31 PM
Well it's in the /items/ directory. I put the .htaccess file in that directory, so I thought, from what I read, all files and sub-directories of /items/ fall under this .htaccess' rules. Do I have to tell it through php somehow that it's acting like that? I thought the whole purpose of .htaccess was so that people can type runenerds.com/items/ITEM NAME and it treats it as runenerds.com/items/view_item.php?Item=ITEM NAME

I just use simple php and mysql statements...I didn't set anything special up.

But like I said. The one that turns the url to runenerds.com/items/view.ITEM NAME works perfectly. It works exactly as if it were runenerds.com/items/view_item.php?Item=ITEM NAME

UnlimitedMB
09-30-2009, 03:39 PM
Replace view_item.php with a phpinfo(); so you can see exactly whats happening.

destrugter
09-30-2009, 03:53 PM
Well that is strange o_O

When I do that, this is what I get under Apache environment (I cut out stuff that may be irrelevant).


REDIRECT_QUERY_STRING Item=gold bar
REDIRECT_URL /items/gold bar
REQUEST_METHOD GET
QUERY_STRING Item=view_item.php&Item=gold bar
REQUEST_URI /items/gold%20bar
SCRIPT_NAME /items/view_item.php


It appears as though it's redirecting to a query string where the item contains the view_item string as well as the item name. That's really strange to me. I wonder why view.gold bar has the right information. I'll do a little more research on that site you referred me to, and see if there is a certain way to call this other than what I did.

EDIT:
I'm getting the idea it's coming from the QSA flag...

UnlimitedMB
09-30-2009, 04:07 PM
Don't put your view_item.php inside the /items/ directory.
Your just creating loops by doing that.

The items directory shouldn't actually even exist, its just a virtual folder.

destrugter
09-30-2009, 04:17 PM
Where would I store my files then? I have a directory for every major part of my site (users, forums, guides, items, etc.)

If I'm not supposed to have items, then I would put it in the root dir...or the root I'm allowed to access. So how would I sort it out? Obviously I don't want it to be runenerds.com/items_index.php

Could you explain a bit further please?

UnlimitedMB
09-30-2009, 05:26 PM
Then you must use a different directory or a subdirectory as the virtual folder.

You are redirecting /items/* to /items/items_index.php
But * includes items_index.php, so you are redirecting items_index.php to itself.
Thats why your script is not working.

destrugter
09-30-2009, 11:52 PM
Ok, I get it now. Thanks, I made a seperate dir to put my files in, and put my .htaccess file in /items/ and it works great! Thanks for the help.