/    Sign up×
Community /Pin to ProfileBookmark

URL Rewriting

Hello everybody,
I’ve to do a simple url-rewriting with an url which looks like

sito.com/{SOMETHING}/{SOMEHING-ELSE-IF-EXISTS} [I]have to be[/I] sito.com/dir/index.php?user={SOMETHING}&page={SOMEHING-ELSE-IF-EXISTS}

Thank you!

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@UAL225Jul 21.2009 — Is it just me, or am I the only one that is not understanding why you posted this here? Is there a problem with that or what? Need Help? There are tons of people that can help you here (not me(still learning the ropes for the most part)) but I don't understand this post as in problem wise.?
Copy linkTweet thisAlerts:
@WiderauthorJul 21.2009 — I asked if anyone can help me to configure the .htaccess file to redirect an url which looks like website.com/{DIRNAME}/{OPTIONAL-OTHER-DIR} to the real url which looks like website.com/dir/index.php?user={DIRNAME}&page={OPTIONAL-OTHER-DIR}?
Copy linkTweet thisAlerts:
@UAL225Jul 21.2009 — ok I just thought that you didnt make that clear enough. I bet some one will be here to help you shortly. ?
Copy linkTweet thisAlerts:
@MindzaiJul 21.2009 — Something like:

<i>
</i>RewriteBase /
RewriteRule ^$ index.php [NC,L]
RewriteRule ^(.+)/?$ index.php?user=$1 [NC,L]
RewriteRule ^(.+)/(.+)/?$ index.php?user=$1&amp;page=$2 [NC,L]
Copy linkTweet thisAlerts:
@WiderauthorJul 22.2009 — Hi!

I've applied your code:
[CODE]
rewriteEngine on
RewriteBase /
RewriteRule ^$ job/paprika0/accounts/index.php [NC,L]
RewriteRule ^(.+)/?$ job/paprika0/accounts/index.php?user=$1 [NC,L]
RewriteRule ^(.+)/(.+)/?$ job/paprika0/accounts/index.php?user=$1&page=$2 [NC,L]
[/CODE]

But if i try to show my $_GET vars with print_r using http://www.cristianoinsola.it/mada/letto/ this will be the result:
[CODE]Array ( [user] => job/paprika0/accounts/index.php ) [/CODE]
as you can see this is'n the expected result (which is user = mada; page = letto)

What's wrong?? ?
Copy linkTweet thisAlerts:
@PierceMooreJul 23.2009 — I am confused here.. Are you trying to make the url look like
[CODE]yoururl.com/index.php?user=mada&page=letto[/CODE]
or
[CODE]yoururl.com/index.php/mada/letto/[/CODE]

?
Copy linkTweet thisAlerts:
@WiderauthorJul 23.2009 — Users should go to the second one (yoururl.com/index.php/mada/letto/) but the real page is located in yoururl.com/index.php?user=mada&page=letto
Copy linkTweet thisAlerts:
@MindzaiJul 23.2009 — What's wrong?? ?[/QUOTE]


Hmmm not sure. To be honest I find mod_rewrite to be really hit and miss, I prefer to capture the entire request and parse it from inside PHP:

<i>
</i>RewriteEngine On
RewriteBase /job/paprika0/accounts

RewriteCond &amp;#37;{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?query=$1 [QSA,L]


[code=php]
$name = null;
$page = null;
if (isset($_GET['request'])) {
$parts = explode('/', $_GET['request']);
switch (sizeof($parts)) {
case 1:
$name = $parts[0];
break;
case 2:
list($name, $page) = $parts;
break;
}
}
[/code]
Copy linkTweet thisAlerts:
@svidgenJul 23.2009 — Hi!

I've applied your code:
[CODE]
rewriteEngine on
RewriteBase /
RewriteRule ^$ job/paprika0/accounts/index.php [NC,L]
RewriteRule ^(.+)/?$ job/paprika0/accounts/index.php?user=$1 [NC,L]
RewriteRule ^(.+)/(.+)/?$ job/paprika0/accounts/index.php?user=$1&page=$2 [NC,L]
[/CODE]

But if i try to show my $_GET vars with print_r using http://www.cristianoinsola.it/mada/letto/ this will be the result:
[CODE]Array ( [user] => job/paprika0/accounts/index.php ) [/CODE]
as you can see this is'n the expected result (which is user = mada; page = letto)

What's wrong?? ?[/QUOTE]



I believe that mod_rewrite is reinjecting the request and dropping the querystring, in this case. So, on the first pass, it does exactly what you want. The second pass breaks it though. You could try something like the following:
rewriteEngine on

RewriteBase /

# this rule prevents rewriting from taking place if the request


# already points at jobs/paprika0/accounts/index.php,


# which in turn prevents the REprocessing a URL. I think this


# rule is unnecessary if these rewrite rules are included in the


# main apache configuration file (or a directly included conf file).


RewriteRule jobs/paprika0/accounts/index.php - [L]

# maintain the rest of the rules as they were:


RewriteRule ^$ job/paprika0/accounts/index.php [NC,L,QSA]

RewriteRule ^(.+)/?$ job/paprika0/accounts/index.php?user=$1 [NC,L,QSA]

RewriteRule ^(.+)/(.+)/?$ job/paprika0/accounts/index.php?user=$1&page=$2 [NC,L,QSA][/QUOTE]

Again, I think reinjection only occurs when you're doing your rewrites out of a .htaccess file.
Copy linkTweet thisAlerts:
@MindzaiJul 23.2009 — ^^^

That looks good except for a couple of alterations I made below:

<i>
</i>rewriteEngine on
RewriteBase /job/paprika0/accounts

# this rule prevents rewriting from taking place if the request
# already points at jobs/paprika0/accounts/index.php,
# which in turn prevents the REprocessing a URL. I think this
# rule is unnecessary if these rewrite rules are included in the
# main apache configuration file (or a directly included conf file).
RewriteRule index.php - [L]

# maintain the rest of the rules as they were:
RewriteRule ^$ index.php [NC,L,QSA]
RewriteRule ^(.+)/([^/]+)/?$ index.php?user=$1&amp;page=$2 [NC,L,QSA]
RewriteRule ^([^/]+)/?$ index.php?user=$1 [NC,L,QSA]


We got there in the end :p
×

Success!

Help @Wider spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...