3

Directories: eventfinder is project folder

  • Localhost
    -- project1
    -- project2
    -- eventfinder
    --- .htaccess

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.

8
  • RewriteRule ^eventfinder/([^/]+)/?$ /index.php?event=$1 [L] you're not taking into account /eventfinder/ at the moment. Commented Jun 6, 2017 at 12:55
  • @CD001 Nothing happened after I changed what you suggested Commented Jun 6, 2017 at 13:01
  • 1
    RewriteEngine On RewriteRule ^eventfinder/([^/]*)$ /eventfinder/index.php?event=$1 [L] Commented Jun 6, 2017 at 13:02
  • ^ Oh yeah, I missed the /eventfinder/ in the resulting path as well :\ ... unless /eventfinder is an actual directory and that's where the .htaccess file resides? Commented Jun 6, 2017 at 13:04
  • Nothing happens with localhost/eventfinder/randomevent123 Commented Jun 6, 2017 at 13:05

2 Answers 2

2

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] 
    
Sign up to request clarification or add additional context in comments.

2 Comments

I changed these settings from httpd.conf and restarted apache, still not working. LoadModule rewrite_module libexec/apache2/mod_rewrite.so I changed <Directory /> AllowOverride All Require all granted </Directory> Apache version 2.4.25
@Troutfisher The AllowOverride needs to be activated for the directory of your DOCUMENT_ROOT. I certainly hope that it is not /.
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.