0

I'm trying to create clean/pretty URL for a website project such that .php extensions will be hidden and I'll be able to convert a URL like

example.com/user.php?profile=john-doe

to

example.com/user/john-doe

this is the code I was able to come up with:

RewriteEngine On
# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]
RewriteRule .* - [L,R=404]

# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]

#setting query for querying pages
RewriteRule ^(.*?)/(.*?)/?$ $1?name=$2 [NC]

but it us not working, I'll be grateful if I can get this fixed or a better complete .htaccess configuration to achieve my aim.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stephynoe
  • 1
  • 1
  • `profile` is a `GET` var in the address string. You cannot eliminate it if you plan to keep it as `GET`. Try using `cookies` to pass the var. There are a ton of tutorials out there that will help you hide the `php` extention – nrm Oct 27 '18 at 20:43
  • Possible duplicate of [how-to-create-friendly-url-in-php](https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php). – Stefmachine Oct 27 '18 at 21:00
  • "but it us not working" - please state exactly what is not working and what happens. The directive that appends the `.php` file extension looks as if it should work - but are you saying this does not work for you? Do you have other URLs that you want to rewrite or create exceptions for? As it stands, your last directive is going to rewrite every URL that has two path segments. – MrWhite Oct 28 '18 at 22:33

1 Answers1

0

Try this is in route.php

$route['default_controller'] = 'your main controller';
$route['your desire variable after project name'] = 'your controller/your function name which is used in code';

like,
$route['contact'] = 'MainCtrl/contact';

you can use only 'contact' to call your function which is available inside the your controller so your link will be converted in shorter form

  • The OP isn't using codeigniter, they aren't using a front-controller and they are unlikely to have a `route.php` file. They are simply trying to create a single "pretty" URL using `.htaccess` and mod_rewrite. – MrWhite Oct 30 '18 at 22:56