It's one heckuva trek to cooler tech

Enable Apache Rewrite For Your Wordpress or Modx CMS Site

02-Jul-10 10:55
If your CMS can't produce SEO-friendly URL's, the problem could be your web server.

Web applications often involve an .htaccess file with directives to rewrite URL's. Content Management Systems (CMS) like WordPress use rewrites to render pages optimized for SEO searches, using names like http://www.mysite.com/the-name-of-my-article.html. I use a different CMS, Modx. Like Wordpress, it provides a ready-made .htaccess file.

However, creating the .htaccess file in your web application's directory is only half the battle. You still have to configure your web server to allow URL rewrites. It's difficult to find concise instructions on these steps, because they vary with the server and the location of your application.

Activating Apache Rewrite

Fortunately, Apache2 is highly modular. You can reconfigure it to allow rewrites for your application by making a couple of quick changes. The following steps have been tested in Ubuntu 10.04. (If you are a shared hosting customer, Apache is probably already configured for you, and you can skip this section.)

1. Enable the rewrite module in Apache:

sudo a2enmod rewrite

2. Edit Apache's custom config file for your site. Unless you're running multiple/virtual sites from the same server, the file you're looking for is /etc/apache2/sites-available/default. If your web server allows access by SSL (https://), the file is default-ssl. Find the stanza for the top-level directory, which looks like this:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Note that the default setting is AllowOverride None. Below it, add some new lines to define an exception for the directory hosting your web application:

<Directory /var/www/modx>
    AllowOverride All
</Directory>

3. Restart the web server:

sudo /etc/init.d/apache2 restart

Correcting Htaccess Mistakes

Should you still have problems, the issue could be in your .htaccess files. A common problem is that you installed your application in a directory different from the DocumentRoot (typically /var/www). For example, say you installed WordPress in /var/www/wp, but you want everyone who goes to your domain to be redirected there. You'll need two .htaccess files: one in the DocumentRoot; the other in your WordPresss subdirectory. First, in the root file, /var/www/.htaccess, you'd add the RewriteCond RewriteRule lines to rewrite requests by tacking on the "/wp" part:

RewriteCond %{HTTP_HOST} ^mydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^/?$ "http\:\/\/mydomain\.com\/wp" [R=301,L]

Then, in the subdirectory file /var/www/wp/.htaccess, you need a RewriteBase and other rules using that subdirectory:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Similarly, if you're using the Modx CMS, you'd create subdirectory /var/www/wp/.htaccess conditions and rules like the following

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /modx/index.php?q=$1 [L,QSA]

Resources

Paul Saletan has a background in product management, survey research, reporting and database development. He lives in Austin, Texas.

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
 
STEPANTARASOV34
Posts: 2
Comment
Re:
Reply #2 on : Thu December 22, 2011, 05:25:56
?????? ???????????, ???, ?????? ?????, ??????? ?????
Anonymous
Posts: 2
Comment
Re:
Reply #1 on : Thu December 22, 2011, 05:25:21
???????? ? ????? - ?????? ??????, ??????? ?????

Recent Posts