priojewo
priojewo

Reputation: 947

Problems with special characters in url

I am working on a client project and I am trying to improve the process of passing variables from a url to php. The url structure of the project looks like the following:

http://xyz.com -> Domain
http://xyz.com/folder -> Folder/File
http://xyz.com/doesnotexist -> Folder/File does not exist
                            -> Pass it as a parameter to index.php Script

htaccess Rules take this parameter "doesnotexist" and make it available in a $_GET variable in index.php.

The variable gets encoded in javascript with encodeURIComponent, the url can be called in a browser and decoded in php with urldecode. This works perfectly.

Now to my problem: When the passed variable contains special chars like a slash "/" or an ampersand "&" it does not work anymore, because the browser thinks he is searching for a subdirectory. e.g. variable: "does/notexist" -> Browser tries to open http://xyz.com/does/notexist. At the moment I'm replacing such characters like a slash with others that are no problems in a url before encoding. So I replace "/" with "," or "&" with ";", encode it and everything is fine. In my php script I decode it and replace "," with "/" and ";" with "&" and so one. This works, but is really ugly, so I am searching for a better way to do it.

The initial url structure can not be changed. Does anyone know a better way to do this? I'm stuck here. One idea would be to base_encode the whole url parameter, but this is not the way I want it, because the url should be readable.

Upvotes: 0

Views: 401

Answers (1)

Jelle Ferwerda
Jelle Ferwerda

Reputation: 1229

Thid is a typical situation where you would use a .htaccess file. \Use mod_rewrite.

from here: howto mod_rewrite every request to index.php except real files but exclude one real directory?

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Upvotes: 2

Related Questions