1

Following is the Folder Structure of my PHP Project

PHPProject (Root)
-->Admin/
-->Config/
-->File1.php
-->File2.php
--> ..
--> ..
-->.htaccess
-->File10.php

I would like to enable RewriteURL for the project that can be accessed in the following manner.

Rule 1: My Domain should always be accessed with www.mydomain.com. If someone tries to access as http://mydomain.com i will have to rewrite the URL to http://www.mydomain.com. For which I am using the following rule on my htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
#RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
</IfModule>

Rule 2: I would like to have a specific URL that points to a page:

http://www.mydomain.com/username to rewrite to http://www.mydomain.com/file4.php?name=username

Rule 3:

I am able to get the first rule to work properly with the above information on .htaccess file, but the problem is I have to hardcode the domain name on it. Would it be possible to specify a generic (regex) so that the rule applies to multiple domain?

Question: I require help to combine Rule 1 + Rule 2 + Rule 3 altogether as rules on my .htaccess file.

Could someone please help me acheive this.

2 Answers 2

1

Rule 1:

RewriteCond %{HTTP_HOST} !^www\.(domain1|domain2|domain3|domain4)\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

or:

RewriteCond %{HTTP_HOST} !^www\.domain1\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.domain2\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.domain3\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.domain4\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

Rule 2

http://www.mydomain.com/username

to

http://www.mydomain.com/file4.php?name=username

RewriteRule ^username$ /file4.php?name=username [QSA,NC,L]

and if you want to make it generic (with alphanum chars):

RewriteRule ^([a-z0-9]+)$ /file4.php?name=$1 [QSA,NC,L]

Rule 3

Get all together:

RewriteCond %{HTTP_HOST} !^www\.(domain1|domain2|domain3|domain4)\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

RewriteRule ^([a-z0-9]+)$ /file4.php?name=$1 [QSA,NC,L]

Hope this helps.

Two hints:

Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Sign up to request clarification or add additional context in comments.

Comments

0

If you only need it for http, this should do.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!^(www\.¦$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

And, For the rest you can use the Front Controller Pattern.

[Idea of Front Controller Pattern is redirects all your request to a php file and decide from there]

Follow the following urls, then if you have any question, you can ask :)

http://www.phppatterns.com/docs/design/the_front_controller_and_php

http://onlamp.com/pub/a/php/2004/07/08/front_controller.html?page=2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.