0

I want to redirect from a link like

index.php?site=recipes&id=2451 to recipes/2451

and in fact in works pretty good beside one thing.

First my whole htaccess

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.bla\.de/ [NC]
#RewriteRule ^(.*)$ http://www.bla.de/cocktails/$1 [R=301,L]


### Startseite
RewriteRule ^$ /cocktails/meine-bar [R=301,L]

### rezepte
RewriteRule ^rezepte\.php$ http://www.bla.de/cocktails/rezepte [L,R=301]
RewriteCond %{THE_REQUEST} /index\.php\?site=rezepte
RewriteCond %{QUERY_STRING} ^site=rezepte$
RewriteRule ^index\.php$ http://www.bla.de/cocktails/rezepte? [L,R=301]

### rezepte/name
RewriteCond %{THE_REQUEST} /index\.php\?site=rezepte&id=
RewriteCond %{QUERY_STRING} ^site=rezepte&id=([a-zA-Z0-9-_,]+)$
#RewriteRule ^index\.php$ http://www.bla.de/cocktails/rezepte/%1? [L,R=301]
RewriteRule ^rezepte/([a-zA-Z0-9-_,]+)$ index.php?site=rezepte&id=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /cocktails/index.php?site=$1 [L]

So, what I posted makes:

index.php?site=recipe to /recipe

and if I uncomment

#RewriteRule ^index\.php$ http://www.bla.de/cocktails/rezepte/%1? [L,R=301]

it also redirects

index.php?site=recipes&id=2451 to recipes/2451

but I get an error 404.

If I comment this line out, the url is index.php?site=recipe&id=2451 but the content is displayed properly. Does someone has an idea whats wrong?

Best regards!

Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50
  • Normally you're supposed to rewrite from the fictional (short) resource names to the actual (index.php?site...) ones. Keep the rewriting internal, and make your pages/output contain the pretty URLs already. – mario Jun 14 '13 at 01:44
  • Do I understand you right: I should rewrite recipe/pina-colda-2451 to index.php?site=recipe&id=2451 ? Or whats the point? But then, how could I avoid the id in the url? – Michael Brenndoerfer Jun 14 '13 at 01:46
  • Depends on your code and DB. If you can deduce the database-internal ID (which shouldn't even be exposed) from just pina-colada, then yes. If you have unique titles, don't bother users with numerics. (And, yes; No, mod_rewrite does not do that title-to-id conversion. That's your codes' task.) – mario Jun 14 '13 at 01:52
  • Look here: http://stackoverflow.com/questions/16555414/using-htaccess-to-remove-php-from-url/16555438#16555438 – Steven Moseley Jun 14 '13 at 02:11
  • Sorry, but it doesn't really help me. Excluding the !-d changes nothing :/ I think that the problem is in this line RewriteCond %{QUERY_STRING} ^site=rezepte&id=([a-zA-Z0-9-_,]+)$ – Michael Brenndoerfer Jun 14 '13 at 02:35
  • @Michael - I don't think you read the whole thing. It teaches you how to properly execute rewrites, and to subsequently read them using a router file. – Steven Moseley Jun 14 '13 at 02:50
  • Yeah, maybe I didn't understand your post. Well, at least now it works but I still have a little problem. My index.php includes every element on the page dependent of the $_GET['site'] but after the redirect to cocktails/my-bar/juices $site isn't just my-bar anymore. $_GET['site'] returns now my-bar/juices. Do you know why? `### q-visions/cocktails/meine-bar/filter RewriteCond %{THE_REQUEST} /cocktails/index\.php\?site=meine-bar&filter= RewriteCond %{QUERY_STRING} ^site=meine-bar&filter=([a-zA-Z0-9-_]+)$ RewriteRule ^index\.php$ /cocktails/meine-bar/%1? [L,R=301]` – Michael Brenndoerfer Jun 14 '13 at 07:20

0 Answers0