1

Hey i want to rewrite my links

mydomain.com/?page=pageName

to

mydomain.com/pageName

I tried with

RewriteEngine On
RewriteRule ^([^/]*)$ /?page=$1 [L]

but it seems to to give an error 500

Fallen
  • 13
  • 3

1 Answers1

0

You have an infinite loop because of your rule.
You must add a condition to avoid it

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(index\.php)?$
RewriteRule ^([^/]*)$ /?page=$1 [L]

EDIT: taking your comments into consideration

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^/(index\.php)?$
RewriteRule ^ - [L]

RewriteRule ^([^/]*)/([^/]*)$ /?page=$1&id=$2 [L]
RewriteRule ^([^/]*)$ /?page=$1 [L]
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • Hmm I tried that and it still seems to return error 500 – Fallen Aug 31 '14 at 09:02
  • Did you try with my code only or do you have other rules ? Also, do you have `index` (like `index.php` or other) in your url or is it simply `/?page=xxx` ? – Justin Iurman Aug 31 '14 at 09:05
  • No I dont have any other rules and yes I have index.php – Fallen Sep 01 '14 at 12:16
  • Still error 500. Could it be something from my server settings ? – Fallen Sep 01 '14 at 12:47
  • I'm sorry, i forgot something in my code. I edited my answer, should be working as expected now – Justin Iurman Sep 01 '14 at 12:55
  • Sorry for asking again, but what if i want to add another variable for example. `index.php?page=news&id=1` i tried adding `RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&id=$2 [L]` but it doesnt seem to work – Fallen Sep 15 '14 at 17:07
  • It should be working, your rule is correct. What url did you try and did not work ? – Justin Iurman Sep 15 '14 at 18:43
  • Do you get an error ? Do you have other rules in your htaccess ? – Justin Iurman Sep 15 '14 at 18:50
  • Actually i made work but for some reason the css is not loading – Fallen Sep 15 '14 at 19:11
  • That's because this rule creates a virtual directory and you're using relative paths for all your html links. Instead, use absolute paths. You can see a similar question (and my answer to solve the problem) here: http://stackoverflow.com/questions/25778481/htaccess-pulling-text-to-left/25787607#25787607 – Justin Iurman Sep 15 '14 at 19:14
  • Hmm I tried with rewritebase and base tag in the header. Even with absolute links it still doesnt load the css. I think it just picks up everything after the root and thinks its a GET variable and rewrites it. This only happens with 2nd variable in the rewrite rule. – Fallen Sep 15 '14 at 19:27
  • You can see my "edit" part (in answer) about it. Should be working as expected – Justin Iurman Sep 15 '14 at 19:47