2

I have following URLs:

http://www.example.com/item?title=titlename&id=5 and 
http://www.example.com/page?title=titlename

I want to convert them like:

http://www.example.com/item/titlename/5
http://www.example.com/page/titlename

Note: the page and item are files (ie: item.php and page.php), I was able to remove the .php extension using the following code:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

Any little help would be appreciated.

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
Harrison O
  • 1,119
  • 15
  • 20
  • Have you already "cleaned" the URL that you publish in your application? – MrWhite Nov 04 '17 at 23:36
  • Possible duplicate of https://stackoverflow.com/questions/6940644/url-rewriting-including-title and https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained – mario Nov 04 '17 at 23:40
  • No, i haven't cleaned it, i need help to do so, any assistance please? – Harrison O Nov 04 '17 at 23:42
  • Well, "fixing" the URL that you display to your users (in your application) is the very first thing you must do. You don't do this in `.htaccess`. You then use `.htaccess` to _internally rewrite_ the "clean" URL back into the "real" URL that your application understands. – MrWhite Nov 04 '17 at 23:52
  • The application is working just fine, i just want to internally rewrite the url from something like http://www.example.com/item?title=titlename&id=5 and http://www.example.com/page?title=titlename to something like this : http://www.example.com/item/titlename/5 http://www.example.com/page/titlename – Harrison O Nov 04 '17 at 23:55

1 Answers1

0

i just want to internally rewrite the url from something like http://www.example.com/item?title=titlename&id=5 and http://www.example.com/page?title=titlename to something like this: http://www.example.com/item/titlename/5 or http://www.example.com/page/titlename.

That should be the other way round... internally rewrite from http://www.example.com/item/titlename/5 to http://www.example.com/item?title=titlename&id=5. And presumably you should be including the .php extension on the target URL? (Don't rely on another directive to do append the file extension.)

Try something like the following:

Options -MultiViews

RewriteEngine On

RewriteRule ^([a-z]+)/([\w-]+)(?:/(\d+))?$ /$1.php?title=$2&id=$3 [L]

This will handle a URL both with and without the trailing ID (eg. /5). However, if you specify a URL of the form /item/titlename (ie. no numeric id), then you will naturally get an empty id URL parameter passed to your script.

Note that MultiViews must be disabled for this to work, otherwise mod_negotiation will rewrite the URL from item to item.php (for example) before mod_rewrite and you won't get the URL parameters passed.

If you specifically need to check that the target file, eg. page.php exists before rewriting then you can include an additional condition to check this. However, I wouldn't have thought this was necessary since you'll get a 404 regardless and checking that the file exists is relatively expensive.

Options -MultiViews

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([a-z]+)/([\w-]+)(?:/(\d+))?$ /$1.php?title=$2&id=$3 [L]

This assumes that your files exist in the document root of your site.

Just to clarify, as mentioned in comments, you should already be linking to URLs of the form http://www.example.com/item/titlename/5 in your application.

MrWhite
  • 43,179
  • 8
  • 60
  • 84