0

In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.

mysite/v1.0/foldername

mysite/v1.0/foldername/param1/

mysite/v1.0/foldername/param1/param2/etc

I tried the following:

RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$    foldername.php [QSA,L]

the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.

.htaccess is in foldername

Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.

Gacci
  • 1,388
  • 1
  • 11
  • 23

1 Answers1

0

You are incorrectly rewriting the rules correct rule according to your need would be like,

RewriteEngine On

# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]

# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2&param2=$3 [L]

Refrences

Community
  • 1
  • 1
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
  • Thanks for the help! But I am still having the same problem ... the resources are not found after rewriting the urls! – Gacci Feb 07 '17 at 07:22
  • Where are you using the rule in root directory or v1.0 directory try them in v1.0 directory. – Abhishek Gurjar Feb 07 '17 at 07:23
  • It;s in foldername. It seems like rewriting the url changes directory! – Gacci Feb 07 '17 at 07:30
  • what? foldername suppose to be a php file as you mention in your example. – Abhishek Gurjar Feb 07 '17 at 07:33
  • Also please provide your original url which you want to get converted into pretty url. – Abhishek Gurjar Feb 07 '17 at 07:34
  • I am sorry, you are right. filename is a php file and the filename is in v1.0 Right now I have it localhost so the url goes like this localhost/v1.0/listings and I would like to be able to have multiple subfolders but still be able to load the resources of course! – Gacci Feb 07 '17 at 07:36
  • What are param1 and param2 then and what is listings now? are you not rewriting to `foldername.php?param1=value` like that? – Abhishek Gurjar Feb 07 '17 at 07:57
  • I want to remove the extension. Params 1 and param2 are just letters and spaces (although I know that spaces in url are encoded) and they are optional. Thanks! – Gacci Feb 07 '17 at 16:01