0

I'm using a Hosting Linux service provided by Aruba to run my website.

At this moment, my website URL design looks like this:

mysite.it/index.php?controller=&action=

and I want to hide index.php and the action parameter so my uri looks like:

mysite.it/controller

My basic folder structure is:

www.mysite.it/
  config/
  controller/
  lib/
  model/
  template/
  .htaccess
  index.php    

Is there a solution to achieve this?

Also, I'd need to keep the original document.URL, because for example in some .js there are functions that search for the action parameter in the URL.

Thanks in advance.

  • Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) –  Jan 06 '19 at 03:26
  • what you really want is a router... – jeremy Jan 06 '19 at 03:34

1 Answers1

0

You could include the following text in your .htaccess file
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

Keep in mind that the rewrite engine must be active in your web server config.

Byron Rodríguez
  • 191
  • 2
  • 10
  • I tried but basically it seems to change nothing... rewrite module is active – codASdru22 Jan 06 '19 at 01:52
  • Have you checked if the configuration in the web server for the directory your web site is located allows Override? – Byron Rodríguez Jan 06 '19 at 01:57
  • Ok, your MVC calls the controllers not as part of the path but as an argument. So I think you culd try writing a RewriteRule for each controller this way: `RewriteRule ^(.*)$ index.php?controller=$1&action= [L]` – Byron Rodríguez Jan 06 '19 at 16:40
  • but is the query string kept this way? or do i just need to add [QSA] – codASdru22 Jan 06 '19 at 16:50
  • it's not working, it redirects everything to the index :( – codASdru22 Jan 06 '19 at 16:53
  • I'm typing "www.mysite.it/controller_name" – codASdru22 Jan 06 '19 at 16:55
  • The RewriteRule will redirect everything you type to `index.php?controller=controller_name&action=`. So I think that you could check if there is a default action in the controller, or if you should include it explicitly. If the second case, you could type something like `www.mysite.it/controller_name/action` and the rewrite rule should include `...&action=$2 [L]` – Byron Rodríguez Jan 06 '19 at 17:06
  • so for example now I should change all previous links like `href=index.php?model=contacts&action=find-us` to `href=contacts/find-us` ? And then the `mod_rewrite` should change the URL to just `www.mysite.it/contacts/find-us` ? Is this how it's supposed to work with your code (I'd need it to work this way)? – codASdru22 Jan 06 '19 at 18:32
  • That's the idea. However it depends on the functionality and logic of your system. In order to stablish a more complex scheme you could use the routing module of your MVC. For example, I use Codeigniter, which has a really handy module to control routing. htaccess helps to simplify, but is not the ideal tool to manage routing. – Byron Rodríguez Jan 06 '19 at 19:48