Forum Moderators: phranque

Message Too Old, No Replies

newbie mod_rewrite question

         

swilliams

8:05 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Hi. I've been looking over mod_rewrite on the apache site as well as through posting from other people in this forum and I'm kind of stuck. It looks like a lot of people use mod_rewrite to convert SE friendly urls to dynamic urls. My question is similar but different.

My directory structure is something like:

/root
index.php
faq.php
/docs
index.php
view.php

What I want to do is give out URLs like:
[mydomain.com...]
[mydomain.com...]

and have content served from:
[mydomain.com...]
[mydomain.com...]

The few attempts I tried didn't work so I doubt if I understand how to write rules. These would have to be in a .htaccess file as I don't have access to the httpd.conf file.

It seems most people do not have actual files in the directory they are modifying as I want people to still have access to:
[mydomain.com...]
[mydomain.com...]

Any ideas on where I could start looking would be appreciated. By the way, I don't want to redirect the user within the browser to the dynamic url unless i absolutely have to.

- Samantha

twist

8:32 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The explanation is here,

[httpd.apache.org...]

but i'll try explaining it my own lamens terms since I am somewhat new to this also.

[example.com...] to [example.com...]

# The lines with '#' are ignored and can be removed in your htaccess file

# Turn on the rewrite engine
RewriteEngine on

# Convert your address
RewriteRule ^fakepath/fakename/1$ /filepath/realname.php?id=1 [L]

Now to break it down,

RewriteRule - how you start a rewriterule
^$ - the path will be inside these characters '^' starts line '$' ends it
fakepath/fakename/1 - your fake path without domain name
/filename/realname.php?id=1 - the path to your file excluding domian name
[L] - Short for 'Last rule' and stops the rewriting process here

# To convert your address example
RewriteRule ^1$ /docs/view.php?id=1 [L]
# or you could use
RewriteRule ^docs/1$ /docs/view.php?id=1 [L]

swilliams

3:54 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Hi... I couldn't get the suggestion to work so I tried doing something like the following in the .htaccess file in the root (main HTML) directory.

Options +FollowSymLinks

RewriteEngine On
RewriteCond %{REQUEST_URI}!-d
RewriteRule ^([^/]+)/?$ /view.php?fulltitle=$1 [R]

I was hoping it would do the following:

A web user tries going to:
[mydomain.com...]

The server checks to see if the directory "howto_modify_your_account" is a directory. If not, it should rewrite the URL as:
[mydomain.com...]

the PHP script searches the DB on the fulltitle param and returns the page. Right now I'm getting a "Internal Server Error". I left the [R] option as I thought it does a redirect and changes the URL in the browser so I can see what is happening.

Not sure why its not working.

- S

jdMorgan

5:02 am on Apr 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Samantha,

Welcome to WebmasterWorld!

You must use


RewriteCond %{REQUEST_[b]FILENAME[/b]} !-d

Also, make sure you have a space between "}" and "!" in your code. Posting code on these forums removes those spaces.

Jim

swilliams

5:21 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Thanks jdMorgan,

It works!

The only problem I have is that if the URL is:
[mydomain.com...]

Returns a file not found (404 error). But:
[mydomain.com...]

... does work correctly. So I think I need to figure out how to add the trailing slash and format the URL before checking it with the code you provided.

Is that the ideal way?

Thanks
-S

swilliams

5:38 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Ok... I think I got it. I added a rule just at the beginning which adds the trailing slash.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)$ $1/
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]+)/$ /view.php?fulltitle=$1 [L]

It seems that the following work:
[mydomain.com...]
[mydomain.com...]

both is processed internally as:
[mydomain.com...]

Does the Rewrite cond and rules above is the best way to do the job or is there a simpler way?

Thanks jd!
- S

jdMorgan

6:42 am on Apr 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks good to me...

Your "newbie to mod_rewrite" status no longer applies. :)

Jim