How to make sure that only a clean url is used all the time?

Assuming my site does something if the url looks like mysite.com/index.php?myparam=test

In .htaccess, I added the following line:

 #RewriteRule ^([az]+)$ /index.php?myparam=$1 [L] 

Which works great! The mysite.com/test url will be redirected accordingly and everything will work

I would like to get rid of the dirty URL so that if one of the keys is manually mysite.com/index.php?myparam=test, it will be redirected to mysite.com/test and it still works without falling into infinite cycle..

+4
source share
2 answers

Well, after some further reading on the net and thanks to Salman's answer, I figured out the answer to my problem :)

the code below will rewrite a dirty url to clear one ...

RewriteCond% {THE_REQUEST} ^. \? myparam = ([a-zA-Z] +).

RewriteRule ^ (. *) $ /% 1? [R = 301, L]

note that the question mark for "/% 1" is very important because placing it (on the target side of the redirect) will clear the query string.

after that the code below redirects the clean url to dirty without changing the url (still stays clean)

RewriteRule ^ ([a-zA-Z] +) $ / index.php? Myparam = $ 1 [L]

this will not cause an infinite loop due to the use of THE_REQUEST, which will only respond to your request, and not to server side redirection (I think)

you may need to read the mod_rewrite syntax manuals to customize it for your own needs.

0
source

If you get the idea of β€‹β€‹β€œrouting,” where you have a mechanism for translating parameters into a URL and retrieving the execution parameters from the entered URL, if someone enters the execution parameters manually, you redirect it to the URL that you get from routing mechanism.

Some information about the routing mechanism in symfony 1.4 structure:

http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05

I wrote a routing mechanism for my university project in a couple of hours, so it can be obtained. If not, the most popular routing library [IMHO] is Horde:

http://dev.horde.org/routes/

0
source

All Articles