Question

I am having trouble generating a friendly url using htaccess. I would like something like this,

Origin: /temp/info.php?j=1&m=2
Destination: /temp/info/1/2/

Based on some other answers on here I used the following htaccess rules,

RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /temp/info\.php\?j=([^&]+)&m=([^&\ ]+)
RewriteRule ^ temp/info/%2/%3/? [R=301,L]

This sends me to the correct URL (/temp/info/1/2/), however I get Undefined index j and m errors. So I added the next bit but it seems to be to none effect,

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?temp/info/([^/]+)/([^/]+)/?$ /temp/info.php?j=$1&m=$2 [L]

Can anyone tell me what I am doing wrong here? The second rule seems to be getting ignored.

Was it helpful?

Solution

Try adding this line on top of your .htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} /info\.php\?j=([^&]+)&m=([^&\s]+)
RewriteRule ^ /~temp/info/%1/%2/? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?:^|/)info/([^/]+)/([^/]+)/?$ /~temp/info.php?j=$1&m=$2 [L,QSA]

Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will server /file.php.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top