Troutfisher
Troutfisher

Reputation: 69

URL Rewrite with parameter and php query

Directories: eventfinder is project folder

User comes to the page http://localhost/eventfinder/ and types 'randomevent123' after /eventfinder/

then php query happens with ?event=randomevent123

$event = $_GET['event'];

$stmt = $conn->prepare("SELECT * FROM events WHERE name = :name");
$stmt->bindParam(":name", $event);
$stmt->execute();

and returns data from database

I am trying to rewrite my url but I don't understand what is the problem...

http://localhost/eventfinder/index.php?event=randomevent123

to

http://localhost/eventfinder/randomevent123

With .htaccess

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?event=$1 [L] 

But query won't work.

Upvotes: 3

Views: 1268

Answers (2)

CD001
CD001

Reputation: 8472

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /eventfinder/index.php?event=$1 [L]

You can us !-f and !-d checks on the requested filename to ensure that the URL that's being typed is not a file and not a directory before doing the rewrite itself - it prevents a recursive loop on index.php

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

Give the following a try:

  1. Confirm that mod_rewrite is loaded
  2. Check that AllowOverride allows htaccess parsing
  3. The following rule (inside eventfinder directory):

    RewriteEngine On
    RewriteBase /eventfinder/
    
    RewriteCond %{THE_REQUEST} ^GET\ /eventfinder/index\.php\?event=([^\s&]+) [NC]
    RewriteRule ^index\.php$ %1 [R,L,QSD]
    
    RewriteRule ^(?!index\.php([^/]+))$ index.php?event=$1 [L] 
    

Upvotes: 2

Related Questions