Variables in friendly Urls

Asked

Viewed 152 times

1

How are you?

I am doing the user profile and there are 3 tabs, IE, three areas that users can navigate. For profile, I rewrote as follows on . htaccess:

RewriteRule ^profile/([0-9]+)/([a-z0-9-]+)/?$ profile.php?id=$1&name=$2 [NC] 

This is not the problem, the problem is that I want to pass GET variables the normal way too, as in the example of tabs, getting something like URL:

profile/245/user-name?tab=about

But it doesn’t read the variable when I put it to test:

echo $_GET['tab'];

Returns the following error:

Notice: Undefined index: tab in C:...\profile.php on line 14

NOTE: In the pages that I rewrite that DO NOT HAVE VARIABLES, it reads normally! I could even include in the . htaccess this variable this way, but imagine you have several of these for little things, like ? Edit=1...

1 answer

2

The problem is that you are completely discarding the query string in its rewrite.

One way out is to capture what comes after ? and include in redirect, something like that:

RewriteRule ^profile/([0-9]+)/([a-z0-9-]+)/?\??(.*)?$ profile.php?id=$1&name=$2&$3
                                        grupo 3 ^                adicionado aqui ^

Basically I am giving an initial notion of logic, need to take other factors into consideration (for example, someone may happen to pass a id or name and your application gets confused if you’re not ready for it).

See working on Regexr. (I used a backslash to escape the normal bars).

A better way is this, posted in a linked question:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?caminho=$1&%{QUERY_STRING} [NC,L]

So you get all the way in $_GET['caminho'] and can split with a simple explode, managing as many levels as needed.


Now, a personal comment. This thing of converting such a "friendly URL" into GET parameters, I think horrible (but that’s how almost everyone does). Some applications made by a "smarter" staff simply take the direct path from the URL, with explode or something like that, avoiding all these problems. I would suggest thinking about this alternative as soon as I master this part of re-writing.

The amazing thing is that I’ve seen people teaching this GET simulation in certain college courses, but since it’s the same people who teach PHP to use only with OOP and MVC, I don’t think it’s weird the rest.

  • It didn’t work... I’ve thought about doing this (adding a third variable, but it’s not very effective I think). As the application end up 'confusing', I was confused haha. How so? In my case, I always validate the URL, I link the user’s name (with a function that transforms this: José Rodrigues, in this, Jose-Rodrigues). I’ve never had a problem with that, but I was worried, could you give me an example? And as for this other method, I think I’ve seen some people using it, so if you could send some referral links, I’d be grateful...

  • If it didn’t work, you need to review the code, I even put a demo in Regexr for you to test "live", with a test URL and everything. As for "effectiveness", one or ten variables will not change the situation. The work of the regex is practically the same in all cases.

  • Change the data in the link above, and see how it is rewritten in the bottom panel: http://www.regexr.com/3dqrb

Browser other questions tagged

You are not signed in. Login or sign up in order to post.