How to make Apache mod_rewrite work?

Hi,

I want to enable Pretty URLs in Concrete5 and Joomla CMSs. So far I have

  • enabled pretty urls in the CMS
  • created .htaccess files (as they showed)
  • changed AllowOverride None to All in /etc/Apache2/httpd.conf <directory>
  • restarted Apache

But the pretty urls still don’t work, When I click any link on either site it gives me a page not found error. If I use the “non-pretty” URLs, all the links work. So I guess Apache either doesn’t read the httpd config file or it doesn’t read the .htaccess files or something else. Has anyone got this working? What else did you do?

For more details on what I’ve tried, please have a look here: concrete5 :: Unable to connect from another computer

Thank you.

Did you also enable rewriting with:

RewriteEngine On

Appropriate place would be in the virtual host config.

Yes, RewriteEngine On is in the .htaccess file. Or should I put it in the httpd.conf as well?

I don’t have a virtual host, I’m running from the htdocs on localhost.

Well, if you think about it, how is the .htaccess file going to be read in when you give a Pretty URL? First it has to find the directory. But the RewriteEngine On and the rules are in the .htaccess file, so the translation from Pretty URL to standard URL can’t be done. Chicken and egg. So the .htaccess rules must either be in a directory that is always read, e.g. DocumentRoot, or in httpd.conf.

“.htaccess rules must either be in a directory that is always read, e.g. DocumentRoot”

What did you mean by DocumentRoot? I’ve tried both places for the .htaccess file: …/htdocs - this is the root of the web server and …/htdocs/C5 - this is the root of the CMS.

Or I should copy everything from the .htaccess file and paste it all in the httpd.conf? Where, in the <directory>? Thank you.

PS. What, 7,179 posts!!! Have you been doing this since 1960? :slight_smile: I thought I’ve used Suse for ages but only have 200 posts :stuck_out_tongue: (of course I admit I don’t live in this forum, but still…)

No, I’m just an automated bot posting answers.

Probably the simplest is to put it in httpd.conf under the <Directory> scope for the whole site. Here’s an example set of rules I use to force https for the site. Your rules would be different of course.

<Directory /srv/www/htdocs>
   ...
   RewriteEngine on
   RewriteCond %{SERVER_PORT} !^443$
   RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
   ...
</Directory>

Thank you, I’ll try that in the config file. One question though: if I have multiple sites running but all from the same root, for example
/srv/www/htdocs/
/srv/www/htdocs/C5
/srv/www/htdocs/Joomla
/srv/www/htdocs/Drupal
etc.

Do I create different rewrite rules for different sites under the same <Directory /srv/www/htdocs> or I create different rewrite rules under different directories in the config, e.g.
<Directory /srv/www/htdocs>
<Directory /srv/www/htdocs/C5>
<Directory /srv/www/htdocs/Joomla>
<Directory /srv/www/htdocs/Drupal>

The rewrite rules affect all URLs that are in the surrounding scope. Therefore if the rules only apply to one web app, it would be best to restrict the scope of the rewrite. However, remember that the correct <Directory> scope can only be determined after the rewrite, so you probably want to use <Location> scope instead to restrict the rewrite rules. If it sounds too complicated, just put the rewrite rule in the top scope; provided rewrite rules don’t clash, which they are not supposed to anyway, because then you would have a Pretty URL that could mean more than one real URL.

Remember the sequence of events:

web browser presents Pretty URL
web server applies rewrite rules that are applicable based on the Pretty URL (not the real URL)
a redirect is generated to the real URL

On 06/02/2010 02:26 AM, linuxoidoz wrote:
>
> Hi,
>
> I want to enable Pretty URLs in Concrete5 and Joomla CMSs. So far I
> have
>
> - enabled pretty urls in the CMS
> - created .htaccess files (as they showed)
> - changed AllowOverride None to All in /etc/Apache2/httpd.conf
> <directory>
> - restarted Apache
>
> But the pretty urls still don’t work, When I click any link on either
> site it gives me a page not found error. If I use the “non-pretty” URLs,
> all the links work. So I guess Apache either doesn’t read the httpd
> config file or it doesn’t read the .htaccess files or something else.
> Has anyone got this working? What else did you do?
>
> For more details on what I’ve tried, please have a look here:
> ‘concrete5 :: Unable to connect from another computer’
> (http://www.concrete5.org/index.php?cID=53064)
>
> Thank you.
>
>

The magic line is in /etc/apache2default-server.conf

In order to make mod_rewrite work, the line:

AllowOverride None

must be changed to:

AllowOverride All

That’s how I always do it.
It allows the rewrite rules in .htaccess files to be taken into account.

I’m using WordPress as CMS in my site.

Vahis

http://waxborg.servepics.com
openSUSE 11.2 (x86_64) 2.6.31.12-0.2-default
09:35am up 29 days 13:47, 13 users, load average: 0.02, 0.05, 0.02

Sorry, I still don’t get it.

Here’s my <directory> from httpd.conf:


# forbid access to the entire filesystem by default
<Directory />
    Options None
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

# use .htaccess files for overriding,
AccessFileName .htaccess
# and never show them
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files>

and here’s my default-server.conf:


DocumentRoot "/srv/www/htdocs"

#
# Configure the DocumentRoot
#
<Directory "/srv/www/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 None
	# 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>

If I simply change ‘AllowOverride None’ to ‘AllowOverride all’, I get “Access forbidden! You don’t have permission to access the requested directory. There is either no index document or the directory is read-protected.”

If I put this:


<Directory "/srv/www/htdocs/linuxoid/c5">
	AllowOverride All
</Directory>

or this:


<Directory "/srv/www/htdocs/linuxoid/c5">
	AllowOverride All
	Order allow,deny
	Allow from all
</Directory>

or this (copying the .htaccess into the config):


<Directory "/srv/www/htdocs/linuxoid/c5">
	AllowOverride All
	Order allow,deny
	Allow from all
	RewriteEngine On
	RewriteBase /linuxoid/c5/
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.*)$ index.php/$1 [L]
</Directory>

into any of the config files, I still get “Access forbidden…” but only for the c5 directory, I can get to the root though.

If I add


<Directory "/srv/www/htdocs">
    RewriteEngine On
    Options None
    AllowOverride all
    Order deny,allow
    Allow from all
</Directory>

to httpd.conf and move .htaccess to /srv/www/htdocs, I can only get to c5 root but all links return “Page not found”, meaning the override doesn’t work.

If I put that block into default-server.conf and move .htaccess to …/c5, I get “Access denied”.

If I add


<Directory "/srv/www/htdocs">
    RewriteEngine On
    Options None
    AllowOverride all
    Order deny,allow
    Allow from all
</Directory>

to httpd.conf and move .htaccess to /srv/www/htdocs/linuxoid/c5, I can only get to all sites roots but all links return “Page not found”, meaning the override doesn’t work.

I’m totally clueless.

If I add


<Directory "/srv/www/htdocs/linuxoid/c5">
    RewriteEngine On
    Options None
    AllowOverride all
    Order deny,allow
    Allow from all
</Directory>

to httpd.conf and move .htaccess to /srv/www/htdocs/linuxoid/c5, I can only get to C5 root but all links return “Page not found”, meaning the override doesn’t work.

If I add


<Directory "/srv/www/htdocs/linuxoid/c5">
    RewriteEngine On
    Options None
    AllowOverride all
    Order deny,allow
    Allow from all
</Directory>

to default-server.conf and move .htaccess to /srv/www/htdocs/linuxoid/c5, I can only get to server root and I get “Access forbidden…”

I think I’ve tried everything.

You should put the Rewrite rules inside the Directory scope also. If you put them inside .htaccess you run into chicken and egg problems unless you understand the steps in the rewrite process. Just forget about using .htaccess for Rewrite rules, alright?

If you see above, I did put it inside the directory.

No, where are the rewrite rules? All you have inside <Directory> is the enabling directive.

Ah, you mean this:


<Directory "/srv/www/htdocs/linuxoid/c5">
    RewriteEngine On
    Options None
    AllowOverride all
    Order deny,allow
    Allow from all
    RewriteBase /linuxoid/c5/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</Directory>

I’ve also deleted .htaccess from …/c5 and I get “Access forbidden…”

Right, that says that URLs of the form

http://your.server.name/linuxoid/c5/something

will get rewritten. Is this what you were expecting? If so, look in /var/log/apache2/access_log and /var/log/apache2/error_log to what the server thought of your URL.

You might also want to set RewriteLogLevel to show you what the rewrite engine is doing:

mod_rewrite - Apache HTTP Server

I couldn’t find anything in the access log. But the error log has these:


[Wed Jun 02 21:12:58 2010] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php5/extensions/php_curl.dll' - /usr/lib64/php5/extensions/php_curl.dll: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php5/extensions/php_gd2.dll' - /usr/lib64/php5/extensions/php_gd2.dll: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php5/extensions/php_mbstring.dll' - /usr/lib64/php5/extensions/php_mbstring.dll: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php5/extensions/php_mysql.dll' - /usr/lib64/php5/extensions/php_mysql.dll: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php5/extensions/php_openssl.dll' - /usr/lib64/php5/extensions/php_openssl.dll: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php5/extensions/php_zip.dll' - /usr/lib64/php5/extensions/php_zip.dll: cannot open shared object file: No such file or directory in Unknown on line 0
[Wed Jun 02 21:12:58 2010] [notice] Apache/2.2.13 (Linux/SUSE) mod_ssl/2.2.13 OpenSSL/0.9.8k PHP/5.3.2 configured -- resuming normal operations
[Wed Jun 02 21:13:18 2010] [error] [client 192.168.1.2] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /srv/www/htdocs/linuxoid/c5/
[Wed Jun 02 21:13:27 2010] [error] [client 192.168.1.2] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /srv/www/htdocs/linuxoid/c5/index.php

See the section Per-Directory rewrites in this document:

mod_rewrite - Apache HTTP Server

I’ll be $#%$@##^&#%^@^%#^&@#^ed!!! I googled for the error and someone sugested to add ‘Options +FollowSymLinks’ to the directory. Well, now it’s all working!

Here’s my httpd.conf directory extract for everyone’s information:


# forbid access to the entire filesystem by default
<Directory />
    Options None
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

<Directory "/srv/www/htdocs/linuxoid/c5">
    Options +FollowSymLinks
    AllowOverride all
    Order deny,allow
    Allow from all
    RewriteEngine On
    RewriteBase /linuxoid/c5/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</Directory>

# use .htaccess files for overriding,
AccessFileName .htaccess
# and never show them
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files>

# List of resources to look for when the client requests a directory
DirectoryIndex index.php index.html index.html.var

So now I’ll undo everything and try it with .htaccess and ‘Options +FollowSymLinks’.

I wonder why it complains about php libraries, everything’s working OK with all those modules.