Mod rewrite Help

New to mod_rewrite? This is a good place to start.

Mod rewrite Help

Postby dannyboy123 » Wed May 20, 2009 3:26 pm

Hello

I'm new to mod rewrite and I'm trying to achieve the following:

www.domain.com/index.php?page=home&&sta ... &type=user

to

www.domain.com/home/test/user

At present the "page" variable calls a file which could be located in any subfolder within domain.com and other variables after this determine actions within the files called in. Can anyone suggest a method to make the URL display correctly whilst still calling in the correct files? There could possibly be other variables after the "page" variable and therefore if the rule was a wildchild so could possibly achieve: /home/test/user/var1/var2/var3/etc.

Ideally I'd like to apply one rewrite rule to the .htaccess file in the httpdocs directory so that all files within here are affected.

Am I trying to achieve something that is impossible? Any help would be much appreciated.

Many Thanks
dannyboy123
 
Posts: 3
Joined: Wed May 20, 2009 3:14 pm
Location: UK

Postby richardk » Thu May 21, 2009 6:57 am

There could possibly be other variables after the "page" variable and therefore if the rule was a wildchild so could possibly achieve: /home/test/user/var1/var2/var3/etc.

What is the current working URL for /home/test/user/var1/var2/var3/etc.?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1&&status=$2&&type=$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dannyboy123 » Fri May 22, 2009 2:47 am

Thanks for your help so far.

The URL can change, depending on the directory in use, so for /home/test/user/var1/var2/var3/etc this could be any directory within domain.com.

What we are looking to achieve is not a redirection, but a masking of the URL, by changing the variables, for example if the URL is:

domian.com/index.php?page=home&&search=test

we want to use mod rewrite not to point to another folder, but simply to make the URL appear in the browser as:

domain.com/home/test

So what will be loaded is the index.php file, but rather than the URL saying index.php?page=home, it will simply display as /home and so on.

Also, we do not know the GET variables apart from the "page" variables, so the next variable could be anything, so can a wildchild be used to apply this for all variables?

As I have little knowledge of mod rewrite, please excuse my ignorance if I'm trying to achieve the impossible.
dannyboy123
 
Posts: 3
Joined: Wed May 20, 2009 3:14 pm
Location: UK

Postby richardk » Fri May 22, 2009 8:32 am

What we are looking to achieve is not a redirection, but a masking of the URL, by changing the variables, for example if the URL is:

domian.com/index.php?page=home&&search=test

we want to use mod rewrite not to point to another folder, but simply to make the URL appear in the browser as:

domain.com/home/test

So if the URL you want is /home/test/user/var1/var2/var3/etc, what is the original URL? /index.php?page=home&&...

Also, we do not know the GET variables apart from the "page" variables, so the next variable could be anything, so can a wildchild be used to apply this for all variables?

Do you mean /about/richardk to /index.php?page=about&&person=richardk and/or /home/something to /index.php?page=home&&different=something?

It is unlikely there could be a rule that worked out the variable name based only on the variable value. You would either need seperate rules (if all /about means the person variable and all /home means the search variable), to name the variables to the same names, to put the names in the URL or to use PHP to work it out.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dannyboy123 » Fri May 22, 2009 8:46 am

Again thanks for your help.

To answer your questions:

1. domian.com/index.php?page=home&&search=test would be the original URL, but we would like to use mod rewrite to mask this and make it appear as follows:

domain.com/home/test - so that in my address window this is what a visitor to the website can see, but actually the original URL is being called.

2.
Do you mean /about/richardk to /index.php?page=about&&person=richardk and/or /home/something to /index.php?page=home&&different=something?


Yes, but the other way round, so that if we link to /index.php?page=about&&person=richardk the URL displayed will be /about/richardk

It is unlikely there could be a rule that worked out the variable name based only on the variable value


The variable name and value will be called in the link, but I would like to use mod rewrite to mask this as per above.

Thanks
dannyboy123
 
Posts: 3
Joined: Wed May 20, 2009 3:14 pm
Location: UK

Postby richardk » Sat May 23, 2009 4:12 pm

1. domian.com/index.php?page=home&&search=test would be the original URL, but we would like to use mod rewrite to mask this and make it appear as follows:

domain.com/home/test - so that in my address window this is what a visitor to the website can see, but actually the original URL is being called.

I wanted an DIFFERENT example, the rest of the second example from your first post. The "working URL" for /home/test/user/var1/var2/var3/etc.

Yes, but the other way round, so that if we link to /index.php?page=about&&person=richardk the URL displayed will be /about/richardk

I know what way round you want it. But if you think about the mod_rewrite, the input is /about/richardk and the "output" (inside Apache) is /index.php?page=about&&person=richardk.

The variable name and value will be called in the link, but I would like to use mod rewrite to mask this as per above.

The new links is important. That is what mod_rewrite gets as input.

Do you mean /about/richardk to /index.php?page=about&&person=richardk and/or /home/something to /index.php?page=home&&different=something?

What i meant was, will the variable names change for each page=, eg.

when page=123 the second variable will always be abc
/index.php?page=123&abc=4
/index.php?page=123&abc=5

and when page=456 the second variable will always be def
/index.php?page=456&def=6
/index.php?page=456&def=7

so there is a nice, matchable pattern or will each page have many different other variable names, eg

/index.php?page=123&abc=8
/index.php?page=123&def=9
/index.php?page=456&abc=10
/index.php?page=456&ghi=11

where it would be impossible to know what the variable name is just from /page_variable/unknown_second_variable/.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 129 guests

cron