htaccess 多级永久链接?

htaccess mulitlevel permalinks?

你好,我想知道你是否可以帮我处理一个特定的 htaccess 文件。

index#?city=antwerpen 应该变成 /antwerpen

detailfeest#?city=antwerpen&id=123456789 应该变成 /antwerpen/123456789

同时强制使用 www,删除文件扩展名和尾部斜线。谁能完善这段代码?

Options +FollowSymLinks
RewriteEngine On

RewriteBase /

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ / [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ .php [L]

# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R=301]

# Css, js, img paths + pretty links
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|img|js|mail)/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)(.*)$ index#?city=
RewriteRule ^([^/]*)(.*)$ detailfeest#?city=&id=
RewriteRule ^(.+)/(admin|css|fonts|img|js|mail)/(.*)$ / [L]

---- 编辑 ----

我想出了如何强制使用相对路径 - 感谢 stack overflow 帮助我更多地理解这一点。为了将来参考,I found this question particularly helpful.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !(admin|css|fonts|img|js|mail).+$
RewriteRule ^(.*)/(admin|css|fonts|img|js|mail)/(.*)$ / [L]


RewriteRule ^index/([a-z]+) index.php?city=
RewriteRule ^detailfeest/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+) detailfeest.php?  city=&id=
RewriteRule ^detailclub-organisatie/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+) detailclub-organisatie.php?city=&id=&club_id=

要删除尾部斜杠,请使用:

RewriteEngine on
RewriteRule (.+)/$ /example/ [L,R=301]

如果位于目录中,请使用:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

要强制使用 WWW:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/ [r=301,nc]

http://index#?city=antwerpen to http://index/antwerpen

RewriteEngine On
RewriteRule ^([^/]*)\.html$ #?city= [L]

http://detailfeest#?city=antwerpen&id=123456789 to http://detailfeest/antwerpen/123456789

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ #?city=&id= [L]