Home > Back-end >  shorten url with htaccess
shorten url with htaccess

Time:10-17

I have an old system and would like to shorten the URL.

At the moment: www.site.com/app/views/accessories.php

I would like to open it like this: www.site.com/accessories.php

I tried to do like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]

RewriteRule ^(.*)$ app/Views/$1 [NC,QSA]

But this error appears "Internal Server Error". What is wrong?

My folder structure is as follows:

enter image description here

As you can see the files are not in the root. They are inside two app/views/ folders.

Because of that the pages are opening with big and ugly links.

www.site.com/app/views/acessorios.php
www.site.com/app/views/clientes.php
www.site.com/app/views/empresas.php

I wish I could open them like this:

www.site.com/acessorios.php OR www.site.com/acessorios
www.site.com/clientes.php   OR www.site.com/clientes
www.site.com/empresas.php   OR www.site.com/empresas

That is, without looking like app/views/ in the url.

CodePudding user response:

1st solution: With your shown samples and attempts please try following .htaccess rules file. Make sure to place your .htaccess rules file along with your app folder(not inside it, along side it). Also make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteBase /app/views/

##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*\.php)\s [NC]
RewriteRule ^ /%1? [R=301,L]

##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*\.php)/?$ /app/views/$1 [QSA,L]


2nd solution: Rules to redirect URLs to www.site.com/acessorios are as follows:

RewriteEngine ON
RewriteBase /app/views/

##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*)\.php\s [NC]
RewriteRule ^ /%1? [R=301,L]

##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /app/views/$1.php [QSA,L]
  • Related