Jump to content
Larry Ullman's Book Forums

Recommended Posts

I have been working on the project in Chapter 2.  So far everything is running well.  I am currently trying to get the mod_rewrite capability in .htaccess working.  But seem to be having problems.

 

Let me explain my setup:

 

My root_document folder is as follows:

C:\Program Files (x86)\Zend\Apache2\htdocs\PHPadvanced\project_1

The changes that i have made to the httpd.conf is as follows:

<Directory "C:/Program Files (x86)/Zend/Apache2/htdocs/PHPadvanced/project_1">
    AllowOverride All
</Directory>

I have placed this at the bottom of my httpd.conf file.  I have not changed the settings that are earlier in this file:

<Directory "C:\Program Files (x86)\Zend\Apache2/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    Options Indexes FollowSymLinks
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
</Directory>

I looked at several forums and there were many suggestions to change this part of the .conf file.  However, I understood that by adding the code that i did add to the end of the file, i have basically just controlled the amount of space that i have opened up to being controlled from the .htaccess file.

 

I have placed my .htaccess file in both the main htdocs root folder, but also in the project_1 root_folder.  This is also where my index.php file is.

 

My contents of the .htaccess file are as follows:

# project_1 .htaccess
<IfModule  mod_rewrite.c>
    RewriteEngine on
    RewriteBase /PHPadvanced/project_1/
    RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</IfModule>

I am now trying to go to my browser and type in

http://localhost/PHPadvanced/project_1/about

 But this is just bringing up the following message.

Not Found
The requested URL /PHPadvanced/project_1/about was not found on this server.

I am struggling as i am not quite sure where i am having problems.  PLEASE HELP!!!

 

 

 

Link to comment
Share on other sites

mod_rewrite is a tricky beast, and it's hard for us to help you debug without having your exact environment setup.

What I would recommend is starting out with a very simple rewrite rule to get the initial setup going.

 

For example, create two HTML files, a.html and b.html. Put some basic text in both so that you can tell the difference between the two. Next, write a rewrite rule like the following:

RewriteRule a.html b.html

It's only static, but it's a good start.

From there, play with the values in the .conf file and your .htaccess file until when you go to a.html, you actually get b.html.

Good luck!

  • Upvote 1
Link to comment
Share on other sites

Thanks for the suggestion.  I have tried this in my ht.access file and it still does not work.  If i understand correctly, when i type

http://localhost/PHPadvanced/project_1/a.html

into my browser, my address bar will remain unchanged, but in actual fact the content should be that of b.html. 

 

I have created a .htaccess file and put it both in my project_1 folder and also in my htdocs folder.  I don't think that it is supposed to be in both of these.  My understanding is that because i placed the following code at the bottom of my httpd.conf file, then i have only allowed apache to accept override from the project_1 folder.  Is it correct that i therefore only need to create a .htaccess file in project_1??

<Directory "C:/Program Files (x86)/Zend/Apache2/htdocs/PHPadvanced/project_1">
    AllowOverride All
</Directory>
Link to comment
Share on other sites

I have now had a small breakthrough, but not totally solved the problem.

 

When i paste the following code directly into httpd.conf file, replacing the original code that i originally placed in the file.  I am now able to view the content of b.html whilst showing a.html in the browser address bar.

 

This suggests that there is something that i am not quite doing right with the .htaccess file.

 

I have also tried pasting the following code into my httpd.conf file and it is working as it is supposed to.

<Directory "C:/Program Files (x86)/Zend/Apache2/htdocs/PHPadvanced/project_1">
RewriteEngine on
RewriteBase /PHPadvanced/project_1/
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</Directory>

So it would seem that this part of the code is not having too much trouble.  The big problem would be how i am getting apache to access my .htaccess file.  I hope this narrows down my problems a little!!

Link to comment
Share on other sites

Have you tried deleting the .htaccess file in the htdocs folder and just using the .htaccess file in the project_1 folder?

Also, have you tried all the various permutations without editing the .conf file?

I would keep trying all the possible combinations until you find one that works.

I think you're real close.

 

In general, I can get mod_rewrite to work fine by simply placing a single .htaccess file in the directory I want to affect. I don't need to edit the .conf file or anything.

Link to comment
Share on other sites

I have deleted the spare version of the .htaccess file.  However, i am still not having any better results.

 

My understanding was that if AllowOverride was set to None in the httpd.conf file, then this would not allow access to the .htaccess file.

 

I have tried playing around by using AllowOverride All in different directories in the httpd.conf file including the .htdocs directory, but still not luck.

 

I am still baffled at how to get .htaccess to work.

Link to comment
Share on other sites

Right, you'll need to set AllowOverride All in the httpd.conf file, and then restart Apache. Like this, but change "/var/www" to your directory:

 

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
Link to comment
Share on other sites

I have changed the Directory for htdocs to AllowOverride All as shown in my below httpd.conf file and restarted my apache server.  I have also shown the configuration of my LoadModule settings.

#
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule authn_alias_module modules/mod_authn_alias.so
#LoadModule authn_anon_module modules/mod_authn_anon.so
#LoadModule authn_dbd_module modules/mod_authn_dbd.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authn_file_module modules/mod_authn_file.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
#LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
#LoadModule authz_owner_module modules/mod_authz_owner.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
#LoadModule cache_module modules/mod_cache.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
LoadModule cgi_module modules/mod_cgi.so
#LoadModule charset_lite_module modules/mod_charset_lite.so
#LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule dav_lock_module modules/mod_dav_lock.so
#LoadModule dbd_module modules/mod_dbd.so
#LoadModule deflate_module modules/mod_deflate.so
LoadModule dir_module modules/mod_dir.so
#LoadModule disk_cache_module modules/mod_disk_cache.so
#LoadModule dumpio_module modules/mod_dumpio.so
LoadModule env_module modules/mod_env.so
#LoadModule expires_module modules/mod_expires.so
#LoadModule ext_filter_module modules/mod_ext_filter.so
#LoadModule file_cache_module modules/mod_file_cache.so
LoadModule filter_module modules/mod_filter.so
LoadModule headers_module modules/mod_headers.so
#LoadModule ident_module modules/mod_ident.so
LoadModule imagemap_module modules/mod_imagemap.so
LoadModule include_module modules/mod_include.so
LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule logio_module modules/mod_logio.so
LoadModule log_config_module modules/mod_log_config.so
#LoadModule log_forensic_module modules/mod_log_forensic.so
#LoadModule mem_cache_module modules/mod_mem_cache.so
LoadModule mime_module modules/mod_mime.so
LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule negotiation_module modules/mod_negotiation.so
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
#LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule speling_module modules/mod_speling.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule status_module modules/mod_status.so
#LoadModule substitute_module modules/mod_substitute.so
#LoadModule unique_id_module modules/mod_unique_id.so
LoadModule userdir_module modules/mod_userdir.so
#LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule version_module modules/mod_version.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User daemon
Group daemon
</IfModule>
</IfModule>
# 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition.  These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#
#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin admin@example.com
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com: 80
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "C:\Program Files (x86)\Zend\Apache2/htdocs"
#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.  
#
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>
#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "C:\Program Files (x86)\Zend\Apache2/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    Options Indexes FollowSymLinks
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
</Directory>
#

I have tried changing both the Directory in blue and the Directory in red to AllowOverride All.  However, neither of these is in fact helping with the situation.  Can you please confirm that my understanding of these is correct: as the Directory in blue appears earlier in the script, this first creates very tight controls over what can Override its settings.  As the red occurs later in the script this is able to create a narrow scope for allowing the override of settings. If at the end of the script or at some point after the Directory in red, i were to add the following code and leave the previous two Directories (red and blue) set to AllowOverride None, then this should also work??

<Directory "C:/Program Files (x86)/Zend/Apache2/htdocs/PHPadvanced/project_1">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>

I have placed the .htaccess file in my project_1 folder.  Here is a copy of my .htaccess file.

# project_1 .htaccess
<IfModule  mod_rewrite.c>
    RewriteEngine on
    RewriteBase /PHPadvanced/project_1/
    RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</IfModule>

I have been looking at various different forums, but I am still not able to find where i am creating a problem.  Thank you for everyone's help.  Can you let me know if my understanding is correct and what other information i need to provide to try and help diagnosis.  Cheers :)

Link to comment
Share on other sites

I have just added the following line to my code and something has changed, but there is still a problem somewhere!!

<Directory "C:/Program Files (x86)/Zend/Apache2/htdocs/PHPadvanced/project_1">
AllowOverride All
Require all granted
</Directory>

I have left the two directories that are shown in blue and red above as 'AllowOverride None' .  It didn't change anything when i set them to All.

 

However, now when i go to my error_log, i am now seeing the following:

crit] [client 127.0.0.1] configuration error:  couldn't perform authentication. AuthType not set!: /PHPadvanced/project_1/about

This is a very different problem to the one that i was encountering before where it previously simply stated that '/about' did not exist.

Link to comment
Share on other sites

Sorry for the late reply.

I did a little exploring, and I found the following (although I'm not sure if it will help you or not):

 

First off, it should be noted that I am using XAMPP locally on Windows 7.

Also, I did not edit my httpd.conf file at all. Here is the current status of the parts of the httpd.conf file that are most likely crucial to you:

#LoadModule request_module modules/mod_request.so
#LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule rewrite_module modules/mod_rewrite.so
#LoadModule sed_module modules/mod_sed.so
#LoadModule session_module modules/mod_session.so
...
<Directory />
    AllowOverride none
    Require all denied
</Directory>
...
<Directory "C:/xampp/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Includes ExecCGI


    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All


    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

 

There are a few interesting things to note here:

1) The mod_rewrite module was not enabled by default (i.e., originally, it had a # at the beginning of that line). What's interesting though is that when I enabled it in my .htaccess file, it enabled it in the httpd.conf file for me.

2) The <Directory /> block blocks access to everything on my web server. However, any <Directory> blocks below that can enable individual directories as necessary.

3) The <Directory "C:/xampp/htdocs"> block allows any directives to go in .htaccess files in the htdocs directory with the AllowOverride All directive, and allows any requests to go through to the htdocs directory with the Require all granted directive.

 

Again, it should be noted that all of this was set by default, and I didn't have to change anything.

Next, I creating a test environment in the following folder:

C:\xampp\htdocs\test\

In that directory, I have the following three files:

.htaccess

<IfModule  mod_rewrite.c>
  RewriteEngine on
  RewriteRule a.txt b.txt
</IfModule>
a.txt
This is a.txt.
b.txt
This is b.txt.
 
Then, I started Apache, and went to the following URL:
http://localhost/test/a.txt
 
When I did that, I saw the following:
This is b.txt.
 
I then changed things in the .htaccess file only slightly to the following:
<IfModule  mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^(about|contact)/?$ b.txt
</IfModule>
 
Note that I didn't have to restart the server for the change to take effect. I again went to http://localhost/test/a.txt, but this time, I saw:
This is a.txt.

 

However, when I went to any of the following URLs, I saw "This is b.txt.":

http://localhost/test/about
http://localhost/test/about/
http://localhost/test/contact
http://localhost/test/contact/

One last thing to note is that I didn't use the RewriteBase directive, as the base directory is always the directory the .htaccess file is in by default (and everything is relative to that directory). As such, I didn't need the RewriteBase directive, and I don't think you do either.

 

As I suggested before, start off simple, and slowly build up until you understand all the working parts.

That help at all?

  • Upvote 1
Link to comment
Share on other sites

Thank you very much for the time that you have given for helping me work on this problem.

 

I'll walk through the results that i came up with.

<Directory "C:\Program Files (x86)\Zend\Apache2/htdocs">
   Options Indexes FollowSymLinks
   AllowOverride All
   
   # ***ANDY*** added below (not in original config)
   Require all granted
    
    # ***ANDY*** commented out below two lines (original config was not commented out)
    #Order allow,deny
    #Allow from all
</Directory>

At this point i got the following error message:

---403 Forbidden--- You don't have permission to access /PHPadvanced/project_1/a.html on this server.

Please note that my .htaccess file now sits in the following folder

C:\Program Files (x86)\Zend\Apache2\htdocs\phpadvanced\project_1
My .htacess file is as follows
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule a.html b.html
</IfModule>

If i include RewriteBase this does not help either. 

 

If it then change my httpd.conf file to the following

<Directory "C:\Program Files (x86)\Zend\Apache2/htdocs">
   Options Indexes FollowSymLinks
   AllowOverride All
  
   # ***ANDY*** added below (not in original config)
   Require all granted

    Order allow,deny
    Allow from all
</Directory>

I now get a 500 internal Server Error message.  If i look at my error.log i see the following error.

[crit] [client 127.0.0.1] configuration error:  couldn't perform authentication. 
         AuthType not set!: /PHPadvanced/project_1/a.html

If i then comment out the Require all granted line from my httpd.conf file i am then once again able to access

http://localhost/PHPadvanced/project_1/a.html

But it would seem that the .htaccess file is not doing anything as it is still showing my content from a.html instead of b.html.

 

 

 

Once again thanks for trying to help me get to the bottom of this.  If nothing appears obviosuly wrong in what i am doing, then I am sure i will stumble across something in the future.  Thank you for your detailed explanation, it has helped me expand my understanding of the things involved.  :)

Link to comment
Share on other sites

Have you tried changing "C:\Program Files (x86)\Zend\Apache2/htdocs" to "C:/Program Files (x86)/Zend/Apache2/htdocs"?

Unfortunately, your problem is very hard for us to debug unless we're sitting at your computer and can look closely at all the files.

 

My last recommendation would be to go back to the original, unedited httpd.conf file, and then follow the steps I outlined above. If that doesn't work, then there's something else going on with your system (likely outside the configuration of your Apache server).

Link to comment
Share on other sites

Thank you for all your help.  I am pretty sure that i have tried the things that you have suggested.  I have also just tried again anyway.  Still no success!!

 

I will move on for the moment, and see what comes down the road.  At the moment as i am developing on 'localhost', I will continue to modify the httpd.conf file and pretend that i am in fact modifying the .htacess file.  From our conversation, i think i am now able to confirm that my understanding would be mostly correct (even though something silly is going on somewhere).

 

:)

Link to comment
Share on other sites

  • 4 weeks later...

Hello there again!!

 

I have now solved my problem and seem to have gotten my .htaccess file to work now.

 

I continued to do some reading on the internet.  I discovered that it seemed that my apache2 installation had done something strange with the file settings.  I don't know whether this is a result of this being installed via Zend Server or whether it is a windows installation problem (or both). 

 

Here are a few examples of the problems i found with some of the configurations: 

ServerRoot "C:\Program Files (x86)\Zend\Apache2"

DocumentRoot "C:\Program Files (x86)\Zend\Apache2/htdocs"

In several places there had been a either a full use of backslashes or a mixed use of backslashes and forward slashes.

 

I found the following link to adjust my settings which basically used all forward slashes

 

 

 

http://www.uow.edu.au/~nabg/WebServer/Chapter3/WindowsConf.html

 

 

 

I am now able to access the settings i do in the .htaccess file.

 

Another little thing that is discovered was the importance of disabling the caching feature in the browser that i was using.  Without this, it seems i was not able to see any changes i had made (even after closing and opening the browser -- i had of course restarted my apache each time i changed any configurations).

 

I am not sure whether i needed to modify all of the slashes in my httpd.conf file.  But after changing all the backslashes to forward slashes for the important files and folders, it seems that everything was sorted.

 

I hope that this might help anyone else who has similar problems.  Also, if anyone could explain to me whether or why my understanding is correct, that would be really cool!! Cheers !!!

  • Upvote 1
Link to comment
Share on other sites

Hi Larry,  i first came across your books when i was going through Modern JavaScript. 

 

At that time I had already been through loads of beginner to intermediate books on php and was having trouble to find a book that really took my knowledge to the next level.  I dove into Advanced PHP Advanced & OOP and thoroughly enjoyed it.  I have just purchased Effortless E-Commerce and this also seems to be extremely well written (although i have become a fan of OOP so chapter 16 would have been a nice addition).

 

Extremely satisfied reader, it has been a pleasure to work through your books. 

Link to comment
Share on other sites

 Share

×
×
  • Create New...