I need a little help on this one.
I’m in the process of making a php page that collects a query string, tests it against a database for matches, and then redirects the user to a different section of the site.
The is how it works currently (without htaccess/mod_rewrite):
- User visits: domain.com/redirect/index.php?slug=Test_1
- The php page sanitizes and looks up ‘Test_1’ in the database and retrieves a destination URL to redirect the user to. (e.g. domain.com/New_Test_1)
- The php page then 301 redirects accordingly.
This part is working fine. However, due to some variables outside of my control, I need to interpret the original URL (using htaccess/mod_rewrite), like this:
- domain.com/redirect/index.php/Test_1
Which still acts the same as:
- domain.com/redirect/index.php?slug=Test_1
(note: yes, the index.php needs to stay in the url.)
I have this working with the following in my htaccess, but I know it could be better:
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ %{DOCUMENT_ROOT}/redirect/index.php?slug=$2 [PT,L,QSA]
Part I need help with…
Some of the old url slugs had forward slashes in them, like this:
- domain.com/redirect/index.php?slug=How_to_Code/Program
Without htaccess, the above still works, but fails with the pretty(ier) url:
- domain.com/redirect/index.php/How_to_Code/Program
With my current htaccess, it only captures the ‘How_to_Code’ part, but ignores everything after it.
So my question is this: how can I restructure my htaccess to grab everything after domain.com/redirect/index.php/(.*)$, including forward slashes?
Edit: this .htaccess is going inside the /redirect directory
If you do this
That will accept forward slashes and underscores. To add anymore allowed characters just add them inside the [] square brackets. Certain characters may need to be escaped so just Google that.