Neto
Neto

Reputation: 113

RewriteRule not working with the given parameter

I did some research and spent my last 2 days trying to make my .htaccess work without success, and I can't fully understand how .htaccess really work.

This is the URL I'm trying to rewrite, using GET in my .php files:

http://localhost/BDsite/tables/table.php?table=Energy

and I want it to be like this:

http://localhost/BDsite/tables/Energy

Well, this is how my .htaccess is written, and its located inside the site folder, which is /BDsite

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^BDsite/tables/([^/]*)$ /BDsite/tables/table.php?table=$1 [L]
</IfModule>

Sadly nothing is happening with my URL.

Upvotes: 0

Views: 46

Answers (1)

jibsteroos
jibsteroos

Reputation: 1391

As opposed to per-server rewrites, it is possible to do rewriting inside sections or .htaccess files at the expense of some additional complexity. This technique is called per-directory rewrites.

The main difference with per-server rewrites is that the path prefix of the directory containing the .htaccess file is stripped before matching in the RewriteRule.

A RewriteBase should be used to assure the request is properly mapped.

(source: apache.org - rewrite intro .htaccess files)

So, using your directory structure, and adding the necessary RewriteBase you'd get the following that should work:

.htaccess file in root folder (BDsite)

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteBase /BDsite/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^tables/([^/]*)$ tables/table.php?table=$1 [L]
</IfModule>

Project structure:

enter image description here

So now if you request: localhost/BDsite/tables/energy,

you'll get served localhost/BDsite/tables/table.php?table=energy (check this by var_dump-ing $_GET in file table.php)

Upvotes: 1

Related Questions