-1

I'm having a bit trouble generating an htaccess file for a wordpress page. I won't delve into the specifics regarding the page but please know the way things are being done is necessary.

I have the following url:

http://beta.ivech.co.uk/index.php/hire-vehicle-detail?id=1

and I would like it to look like the following:

http://beta.ivech.co.uk/hire-vehicle-detail/1

I've managed to remove the index.php with the standard Wordpress htaccess file but I'm struggling with the pretty URL side of things.

Here's what I have at the moment:

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

I've tried other rules generated online to do the pretty url stuff but had no luck. Truth be told, I'm not entirely sure where I would even put the rule within the above code. Any help is greatly appreciated.

user1530205
  • 302
  • 6
  • 19

1 Answers1

2

Try the following (verified by http://martinmelin.se/rewrite-rule-tester/):

RewriteRule ^hire-vehicle-detail/(.+)$ index.php/hire-vehicle-detail.php?id=$1

see also http://wettone.com/code/clean-urls

Andreas Schrammel
  • 463
  • 1
  • 6
  • 11
  • Hi Andreas, I've tested that using an online testing service and yes that works perfectly yet when I put this into my htaccess file it still won't work. Where would you put that line in the referenced code, should it be before or after the lines for the index.php? – user1530205 Oct 27 '16 at 09:54
  • Try to add it before `RewriteRule . /index.php [L]` - if it doesn't work, where are you redirected to instead? – Andreas Schrammel Oct 27 '16 at 09:56
  • I've added it as you suggested and it is causing a 500 server error – user1530205 Oct 27 '16 at 09:59
  • @User1530205 actually you have to add it before the RewriteConds to avoid infinite redirects caused by the last rule. – Amit Verma Oct 27 '16 at 10:04
  • Yeah @starkeen is right - I've overssen the conds - add it before the conds - otherwise it will only match if the conds are met and as starkeen already said, the last rule fires then every time and redirects you in an infinite loop – Andreas Schrammel Oct 27 '16 at 10:08
  • Hi thanks, that has stopped the server error, however, i'm still having problems. The reason I have a problem is I'm trying to reference the $_GET['id'] value which it is currently stating "Notice: Undefined index: id" I'm assuming I should be able to do that with this rewrite. is that correct or am I doing something completely stupid? – user1530205 Oct 27 '16 at 10:09
  • Am I right, when i assume, that `hire-vehicle-detail` is an own php-file? In this case add `.php` between `hire-vehicle-detail` and `?id=$1` resulting in `RewriteRule ^index.php/hire-vehicle-detail/(.+)$ index.php/hire-vehicle-detail.php?id=$1`. Just a guess, but maybe it helps – Andreas Schrammel Oct 27 '16 at 10:12
  • Thanks again but no the .php is not necessary, I've tried it anyway to be sure but if I access the actual url I'm trying to get to; http://beta.hirehere.co.uk/index.php/hire-vehicle-detail?id=4 then the page actually works. That link is the actual site I am working on by the way just in case that helps clarify anything – user1530205 Oct 27 '16 at 10:16
  • Clicking your link (beta.hirehere.co.uk/index.php/hire-vehicle-detail?id=4) brings me to beta.hirehere.co.uk/index.php/hire-vehicle-detail/?id=4. So you have a folder `hire-vehicle-detail` and in there you have an `index.php` where you try to `$_GET['id']` as far as i see that. So try as rewrite `RewriteRule ^index.php/hire-vehicle-detail/(.+)$ index.php/hire-vehicle-detail/?id=$1` (notice the `/` before `?id=$1`) – Andreas Schrammel Oct 27 '16 at 10:23
  • Hi Andreas, Thanks, I noticed that myself and tried that also, doesn't work :-( – user1530205 Oct 27 '16 at 10:25
  • Do you know how to echo the rewritten url within the PHP file, I can try that to see what is coming out the other end – user1530205 Oct 27 '16 at 10:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126815/discussion-between-andreas-and-user1530205). – Andreas Schrammel Oct 27 '16 at 10:27
  • I've marked this answer as correct, it was helpful in getting me to the final solution. the only change I ended up making was to remove the first 'index.php' from the rewrite rule leaving me with **RewriteRule ^hire-vehicle-detail/(.+)$ index.php/hire-vehicle-detail.php?id=$1** – user1530205 Oct 27 '16 at 10:38
  • As there was the problem with the leading index.php, I've updated my answer to the Rule that worked for @user1530205 – Andreas Schrammel Oct 27 '16 at 10:41