user2827678
user2827678

Reputation: 39

How do I redirect non-existant subdirectory in my index.php file, keeping the URL

I want to basically be able to goto, for example: www.example.com/my/folder/ where the subdirectories /my/folder/ don't exist, and have it redirect to index.php?path=/my/folder, while still keeping the original URL.

I hope this makes sense. I basically have no idea where to start with this. I'm guessing it's something with .htaccess, or http.conf or something, but I have no clue.

I basically want some direction on where to figure out how to do this, or some suggestions.

Upvotes: 1

Views: 144

Answers (2)

will
will

Reputation: 1511

It depends on what web server you're running. If you're running Apache, then yes, it's in the .htaccess file or the VirtualHost file. In addition, mod_rewrite will need to be enabled on your Apache server for this to work.

sudo a2enmod mod_rewrite
sudo service apache2 restart

Other web servers will have it in different locations and require different commands.

Upvotes: 1

Machavity
Machavity

Reputation: 31644

I always liked borrowing from the Wordpress .htaccess file myself

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This little bit of code will rewrite anything that is not a file and send it to your index.php file. You can then parse $_SERVER['REQUEST_URI'] for anything you need from the URL itself.

Upvotes: 3

Related Questions