Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache rewrite rules issue for specific pages

I have problem with my .htaccess redirections. When I type:

http://www.domain.com/contact

it goes to the index.php and not the contact.php

here's my .htaccess:

Redirect 301 /clients http://clients.domain.com

RewriteEngine On
SetEnvIf Host ^www\. page=www
SetEnvIf Host ^mob\. page=mobile

RewriteBase /
SetEnvIfNoCase User-Agent "^Wget" bad_bo
#etc ... 
Deny from env=bad_bot

RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301]

RewriteRule ^about/?$ about.php 
RewriteRule ^contact/?$ contact.php 

rewriterule ^(.*)$ index.php?subdomain=%{ENV:page}&page=$1 

in my php i get:

<?php

print_r($_GET);
Array (
  [subdomain] => www
  [page] => contact.php
) 

What am I missing?

like image 726
aki Avatar asked Nov 24 '11 18:11

aki


People also ask

What is $1 in Apache rewrite rule?

Yes. In the RewriteCond , it's basically saying that it'll run the rewrite as long as $1 doesn't equal on of the files listed to the right of the condition.

What is RewriteRule * F?

RewriteRule "\.exe" "-" [F] This example uses the "-" syntax for the rewrite target, which means that the requested URI is not modified. There's no reason to rewrite to another URI, if you're going to forbid the request.

How do I rewrite an Apache rule?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.


1 Answers

Try this rule:

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

RewriteRule ^about/?$ about.php [NC,QSA,L]
RewriteRule ^contact/?$ contact.php [NC,QSA,L]

rewriterule ^([a-z0-9]+)$ index.php?subdomain=%{ENV:page}&page=$1 [NC,QSA,L]

I also added the NC, QSA, L flags to make sure the last rule [L] is executed if match, [NC] for non case and [QSA] for Query String Append.

like image 92
Book Of Zeus Avatar answered Sep 28 '22 00:09

Book Of Zeus