htaccess vs. VirtualHost

Discuss practical ways rearrange URLs using mod_rewrite.

htaccess vs. VirtualHost

Postby jvolk » Tue Jun 09, 2009 9:58 am

Hi all,

I'm new to mod_rewrite and I'm having a problem where I need to move some rules from a virtualhost to an .htaccess file. AllowOverride is set to None in my main conf file. My virtualhost looks like:

<VirtualHost *:80>
ServerAdmin myemail@email.com
DocumentRoot /to/my/website
ServerName mywebsite.com
#RewriteEngine on
#RewriteRule /cybercamp(.*) "http://mywebsite.com/modules/cyber/index.php?id=35"
#RewriteCond %{HTTP_HOST} ^www.mywebsite.com$ [NC]
#RewriteRule ^(.*)$ http://mywebsite.com$1 [R=301]
</VirtualHost>
<Directory /to/my/website>
AllowOverride All
</Directory>

Everything works just fine when using the rules above.

I moved them into an .htaccess file and using redirects, etc. seem ok too. However, not all my RewriteRules work. Here's the .htaccess:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www.mywebsite.com$ [NC] #Works
RewriteRule ^(.*)$ http://mywebsite.com$1 [R=301] #Works
RewriteRule /cybercamp(.*) "http://mywebsite.com/modules/cyber/index.php?id=35" # Doesn't Work

RewriteRule ^(.*)\.html$ $1.php [NC] #Works

Any suggestions?

Thanks in advance!
jvolk
 
Posts: 1
Joined: Tue Jun 09, 2009 9:49 am

Postby richardk » Tue Jun 09, 2009 3:29 pm

In a .htaccess files that part of the URL matched in the RewriteRule si relative to the directory the .htaccess file is in. For example, for /abc/.htacess and a request for http://example.com/abc/def/?ghi-jkl the RewriteRule would match def/ only.

So /cybercamp should be cybercamp.

Try
Code: Select all
Options +FollowSymlinks

RewriteEngine On

RewriteRule ^cybercamp(/.*)?$ http://example.com/modules/cyber/index.php?id=35 [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?(example\.com)$ [NC]
RewriteRule ^(.*)$ http://%2/$1 [R=301,L]

RewriteRule ^(.+\.)html$ /$1php [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 14 guests

cron