Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I re-write my URLs like this and is it a good idea?

Is it possible to re-write (Apache Mod-Rewrite) a URL from this:

http://www.example.com/view.php?t=h5k6 to this http://www.example.com/h5k6

The reason for this re-write is that the URL needs to be very short (a bit like a tiny URL service).

Would that new URL still hit my view.php page? Would it be able to still make use of the super global array GET ($_GET) to access the variable t? I still want my index.php page to map to this http://www.example.com.

I would also appreciate comments on effects this may have as I am a bit of a noob. :)

Thanks all

like image 258
Abs Avatar asked Dec 08 '22 07:12

Abs


1 Answers

The terminology of mod-rewrite works the other way. Requests would come in like this http://www.example.com/h5k6 and would be rewritten to http://www.example.com/view.php?t=h5k6 internally. That way your PHP scripts can respond to the requests as if they were sent as GET parameters, but users see the URLs in a much friendlier way.

So you don't have to change any PHP scripts, and you can still access the parameter t from the GET array.

In your case, the rule would look like this:

RewriteRule ^/(.*) /view.php?t=$1

You might want to be more specific about what you accept (instead of just the catch-all .* expression). Also, you might want exceptions to this rule if you have other pages in this directory other than view.php.

like image 163
Welbog Avatar answered Feb 12 '23 08:02

Welbog