Clean URLs for a PHP rookie

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you do a

Code: Select all

find / -name mod_rewrite.so
to see if the module is on the system?
Bill_VA
Forum Commoner
Posts: 26
Joined: Sat Mar 03, 2007 8:07 pm

Post by Bill_VA »

Wow, I didn't know I could do that (I told you I was new to Linix).

I found it in two places:
/usr/lib/apache2-prefork/mod_rewrite.so
/usr/lib/apache2/mod_rewrite.so

mod_rewrite doesn't appear anywhere in the httpd.conf file.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Now you need to look for a directory called conf.d. It is usually in /etc/httpd. Typically it will contain several .conf files, one of which would be the one that includes the mod_rewrite module. Before it is parsed though, apache needs to know the path of the modules directory, which will be in httpd.conf.

Keep in mind that I am very used to Red Hat systems, and this file structure could very easily be different than yours. If you do a find for \*httpd\* or \*apache\* or do a whereis httpd, it might tell you where everything is on your system as it relates to apache. Another thing you can do is find httpd.conf.
Bill_VA
Forum Commoner
Posts: 26
Joined: Sat Mar 03, 2007 8:07 pm

Post by Bill_VA »

I think I'm getting closer.

I have a file called loadmodule.conf in the /etc/apache2/sysconfig.d folder that lists the modules to load and "LoadModule rewrite_module /usr/lib/apache2-prefork/mod_rewrite.so" was not in the list.

There is a file in /etc/sysconfig/apache2 that lists all the modules to load on line 97, but rewrite wasn't in the list. I added it, restarted Apache and now mod_rewrite is in the loadmodule.conf file.

But I still get a 404 error no matter what I do to the .htaccess file. What am I doing wrong?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post your entire .htaccess file so we can review it.
Bill_VA
Forum Commoner
Posts: 26
Joined: Sat Mar 03, 2007 8:07 pm

Post by Bill_VA »

Here's what I have in my .htaccess file:

Code: Select all

RewriteEngine On 

RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /mysite/index.php?city=$1&page=$2&function=$3 [NC]
The .htaccess file is not in the server root, it's in a folder named "mysite" (/srv/www/htdocs/mysite/.htaccess)

I've tried it all sorts of ways:
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /index.php?city=$1&page=$2&function=$3 [NC]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?city=$1&page=$2&function=$3 [NC]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php [NC]
RewriteRule ^mysite([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /mysite/index.php?city=$1&page=$2&function=$3 [NC]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, before you get too far into this, try this...

Code: Select all

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /mysite/test.html [QSA,L]
</IfModule>
Make a file called test.html and give it this code:

Code: Select all

<html>
<head>
<title>This is a rewrite test</title>
</head>

<body>
This is a rewriter test.
</body>
</html>
Before testing, make sure in your httpd.conf setup that the following are in either the main <Directory /> tag or in the <Directory> tag of the virtual host you are testing (if you do not know what a virtual host is, then you are probably not using one locally).

Code: Select all

<Directory />
   Options FollowSymLinks
   AllowOverride All
</Directory>
If you change the httpd.conf, restart apache, then try to go to your development site /sometext/ and it should show the url as yoursite /sometext but load the test.html page. If it doesn't, there are problems that may not be associated with your rewrite rules.
Bill_VA
Forum Commoner
Posts: 26
Joined: Sat Mar 03, 2007 8:07 pm

Post by Bill_VA »

I gotta tell you Robert, this is getting frustrating, I'm glad you're there helping.

Still just a 404 error, I had to add the "Options FollowSymLinks" part to the <Directory> tag in the httpd.conf file, I restarted Apache, and still nothing.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, what URL are you navigating to? even if it is localhost, please post the entire URL.
Bill_VA
Forum Commoner
Posts: 26
Joined: Sat Mar 03, 2007 8:07 pm

Post by Bill_VA »

http://192.168.2.5/mysite/blah from my Windows machine on the same network, or http://127.0.0.1/mysite/blah / http://localhost/mysite/blah from the local Linux box
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So given that, you should have pages for the site 'mysite' living in the Document root for that server. If you are hitting it with localhost or 127.0.0.1, the you are using the default server root and document root (unless you set up local virtual hosts, which I am assuming you didn't), so your directory structure for the location of these files is something like

Code: Select all

/
  |
  /var
    |
    /www
      |
      /html
        |
        /mysite
In that mysite folder you should have a .htaccess file, and at the very least a test.html page. Am I correct at this point?
Bill_VA
Forum Commoner
Posts: 26
Joined: Sat Mar 03, 2007 8:07 pm

Post by Bill_VA »

That is correct, I have the .htaccess file in the /mysite folder. No virtual host (I don't even know how to do it).

The directory stucture looks like:

Code: Select all

/ 
  | 
  /srv
    | 
    /www 
      | 
      /htdocs (this is the server root)
        | 
        /mysite (the root for this site)
          .htaccess
          test.htm
          index.php
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is the test page .htm or .html? Also, is it the same in the .htaccess file as it is in real life?
Bill_VA
Forum Commoner
Posts: 26
Joined: Sat Mar 03, 2007 8:07 pm

Post by Bill_VA »

It's all .htm, I got in the habit many years ago of just using .htm instead of .html. The .htaccess file matches
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Call test.htm directly. Does it load?
Post Reply