I have a MVC app in PHP and it's working well, but I want to have a feature where I can mask URLs, for example I have this URL localhost:1900/index/register
which calls from the index.php
controller the method register
.
However, I only want to show localhost:1900/register
as URL and still do the same thing, call from index.php
the register
method, any ideas on how can I obtain that? I am using Apache with htaccess.
This is the content of htaccess:
RewriteEngine On
php_flag display_errors on
php_value short_open_tag 1
php_value upload_max_filesize 1000M
php_value post_max_size 1000M
php_value max_input_time 300
php_value max_execution_time 300
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You should be able to re-write that in your .htccess on Apache, try:
RewriteRule ^(.*)$ /index/$1/ [L,QSA]
to rewrite all to index, or:
RewriteRule ^register$ /index/register/ [L,QSA]
to rewrite only register
What web server are you using? in apache, you can enable mod_rewrite and set rewrite rules in .htaccess. In IIS you must install IIS Url Rewrite extension and then set the rules almost the same way as for PHP.
here's a small example from a web.config file I made some time ago:
<rewrite>
<rules>
<rule name="Riscrivi URL index" patternSyntax="ECMAScript" stopProcessing="false">
<match url="([a-zA-Z0-9_]+).php" />
<action type="Rewrite" url="index.php/Pages/{R:1}" appendQueryString="true" logRewrittenUrl="false" />
<conditions logicalGrouping="MatchAny">
</conditions>
</rule>
</rules>
</rewrite>
I used this file in a CodeIgniter installation, hiding "index.php" from the client point of view, and dinamically adding it to urls ppointed by the client to match correct controller methods