robolist
robolist

Reputation: 149

Multiple mod_rewrite rules

I have successfully created my mod_rewrite rule to change all dynamic URLs on the top level of the site, but now i need to create a rule for a second level, and i think later down the line i might need a second, second level of rewrites.

At the moment i have this

    RewriteEngine on 
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^(.+)$ index.php?subj=$1

And this helps to change /index.php?subj=home to /home as iut does for all other pages such as /contact /about /events and so on.

But now I have created sub pages under events and therefore need to change /events.php?event=event-name to /event-name. But when i add another rule it messes up the whole site. What i tried to do is this

    RewriteEngine on 
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^(.+)$ index.php?subj=$1
    RewriteRule ^(.+)$ event.php?event=$1

But that didnt work.

But on top of all that i want to redirect index.php and / (the root) to /home

Could anyone please show me the correct rules as I have searched around but I just can't seem to get it right.

Many thanks in advance :)

Cheers

UPDATE: Thanks for all your help so far, ive tried everything but can't seem to get it right. As suggested by Ben I will give a bit more info on the URLs. Right now the entire site is sitting in a sub-directory as for now its still under development, so for now it is in mydomain.com/newwebsite/event.phpevent=2 But the .htaccess file currently in the root folder of the development site so it is in /newwebsite directory. So the URL that i am trying to write would be mydomain.com/newwebsite/event/2

You notice it says '2' and that is simply the page/event id. further down the line it will not be the id but rather its title.

Upvotes: 2

Views: 2266

Answers (3)

Ben Carey
Ben Carey

Reputation: 16968

You are testing for the same condition twice, you need to differentiate the regex to test for a unique feature.

I would rewrite the file similar to this:

# Turn on the rewrite engine
RewriteEngine On

# Ignore existing files and directories
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

# Set the general first level rewrites
RewriteRule ^home/?$ index.php?subj=home [NC]
RewriteRule ^event/(.+)$ event.php?event=$1 [NC]

Or alternatively you could work it hierarchically like the following. This will use the first match:

# Turn on the rewrite engine
RewriteEngine On

# Ignore existing files and directories
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

# Set all the rewrites
RewriteRule ^event/(.+)$ event.php?event=$1 [L]
RewriteRule ^(.+)$ index.php?subj=$1 [L]

Upvotes: 2

A.M. D.
A.M. D.

Reputation: 928

It looks to me like your 2nd sample has two rewrite rules trying to do catch the same thing (^(.+)$) and send the request to two different places:

    RewriteRule **^(.+)$** index.php?subj=$1
    RewriteRule **^(.+)$** event.php?event=$1

You need somethin to differentiate them in order for any rule to fire:

    RewriteRule **^home/(.+)$** index.php?subj=$1
    RewriteRule **^events/(.+)$** event.php?event=$1

OR you need to send the request to a single file/page/handler - a front controller that can handle the logic for you and present the correct content.

Upvotes: 0

Clarence
Clarence

Reputation: 2964

The easy way to do this would be to link to /event/eventName instead of only /eventName. This way you can put the logic in the .htaccess:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/event/(.+)$ event.php?event=$1 [L]
RewriteRule ^(.+)$ index.php?subj=$1 [L]

The [L] here tells that processing should stop after reaching match.

The other way would be to send all requests to index.php?subj= and in index.php have the logic to decide if you need to process it as an event (ie. is there an event with such a name).

Upvotes: 0

Related Questions