2017-09-18 2 views
1

Ich brauche Eitelkeit Urls zu implementieren und das ist mein .htaccess DateiVanity URLs mit .htaccess

# check if mod_rewrite is present 
<IfModule mod_rewrite.c> 

    #turns it on 
    RewriteEngine on 

    #if the requested url isn't a file or a dir 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

    #if the request does not end with .gif, .png or .jpg 
    RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC] 

    #process book.php, no matter what was in the url 
    RewriteRule ^(.*)$ /book.php?book_id=$1 [L,QSA] 

</IfModule> 

Diese Übersetzung korrekt funktioniert.

www.mydomain.com/foo -> www.mydomain.com/book.php?book_id=foo 

Jetzt brauche ich eine bestimmte Bedingung:

www.mydomain.com/foo/readers -> www.mydomain.com/book.php?book_id=foo&readers=1 

Während readers nicht ändert, ist foo eine zufällige Zeichenfolge, und ich möchte diese spezifische URL

www.mydomain.com/foo/readers -> valid 
www.mydomain.com/foo/readers?q=hello -> valid 
www.mydomain.com/foo/reader -> invalid 
www.mydomain.com/foo/readers/bar -> invalid 
www.mydomain.com/foo/bar/readers -> invalid 

#I tried many solutions like this but i can't get the desired result 
RewriteRule ^(.*)[/]readers /book.php?book_id=$1&readers=1 [L,QSA] 

Antwort

2
überprüfen

Sie müssen den Anker in Ihrer Regel verwenden und einen Schrägstrich als optional festlegen:

RewriteRule ^([^/]+)/readers/?$ /book.php?book_id=$1&readers=1 [L,QSA,NC] 
+1

Das ist perfekt. Danke – lubilis

+0

Hallo, ich habe ein Problem, wenn ich versuche, den Hash auf dieser URL bei einem Klick zu ändern: 'www.mydomain.com/foo/readers ->' window.location.hash = '#etwas'; 'Ich kann verstehe nicht, warum 'www.mydomain.com/foo/readers/# something' oder' www.mydomain.com/foo/readers # something' umgeleitet werden. – lubilis

+0

Ich bin unterwegs, da ich die nächsten Tage reise. Auf welches Problem stoßen Sie nach der Ausführung des JS-Codes? Wird eine Anfrage an den Webserver ausgelöst oder nicht? – anubhava