Can I rewrite my urls like this and is this a good idea?

Is it possible to rewrite (Apache Mod-Rewrite) URL from this:

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

The reason for this rewrite is that the URL should be very short (a bit like the tiny URL service).

Will the new url still get to my view.php page? Would it still be possible to use the super global array GET ( $_GET ) to access the variable t ? I still want the index.php page to display on this http://www.example.com .

I would also appreciate comments on the effects that might have, since I'm a little noob. :)

Thank you all

+4
source share
3 answers

Mod -rewrite terminology works differently. Requests will be submitted as follows http://www.example.com/h5k6 and will be rewritten internally http://www.example.com/view.php?t=h5k6 internally. This way, your PHP scripts can respond to requests as if they were sent as GET parameters, but users see URLs in a much more friendly way.

Therefore, you do not need to change the PHP scripts, and you can still access the t parameter from the GET array.

In your case, the rule will look like this:

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

You might want to clarify what you accept (and not just the catch-all .* Expression). In addition, you may want exceptions to this rule if you have other pages in this directory other than view.php.

+12
source

Try one of the following:

 # /view.php?t=h5k6 externally to /h5k6 RewriteCond %{THE_REQUEST} ^GET\ /view\.php RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$ RewriteRule ^/view\.php$ /%2 [L,R=301] # /h5k6 internally to /view.php?t=h5k6 RewriteRule ^/([0-9a-z]+)$ view.php?t=$1 [L] 
+2
source

Yes, it is definitely possible and will have exactly the effect that you are describing. However, your terminology is the opposite. A "short" URL is one that sends the source code of the browser to the server, and it is rewritten to a longer version, which is then actually processed and leads to a PHP request.

To a large extent, the only catch is that using it carelessly can cause search engines to index both URLs and consider their duplicate content, which is bad for your ranking. On the one hand, this can be avoided (and should) be avoided by never publishing the โ€œlongโ€ version anywhere (i.e., all internal links on the site have a โ€œshortโ€ format). However, since you can publish them randomly (which can happen easily), you should also use the rel = "canonical" link to tell search engines that the URL to which you want to index pages.

+1
source

All Articles