[英]Rewrite partial URL to specific page with .htaccess mod_rewrite
真的無法找出標題,抱歉,這不是最好的方法。
我正在尋找這樣重寫URL:
https://awesomechristianmusic.com/song-list?artist=dc-talk
看起來像這樣:
https://awesomechristianmusic.com/dc-talk
或針對類型/主題:
https://awesomechristianmusic.com/song-list?genre=rock
https://awesomechristianmusic.com/song-list?topic=forgiveness
看起來像這樣:
https://awesomechristianmusic.com/genre/rock
https://awesomechristianmusic.com/topic/forgiveness
不幸的是,我的.htaccess文件似乎存在一些問題,我無法弄清楚。 這是我正在使用的:
RewriteEngine on
RewriteBase /
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,QSA,R=301]
# Remove www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L,QSA]
# Remove trailing slashes
RewriteCond %{REQUEST_URI} ^.*/$
RewriteRule ^(.*)/$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
# This is where I'm currently working
RewriteRule ^/([^/]+)$ /song-list?artist=$1 [NC,L,QSA]
RewriteRule ^/genre/([^/]+)$ /song-list?genre=$1 [NC,L,QSA]
RewriteRule ^/topic/([^/]+)$ /song-list?topic=$1 [NC,L,QSA]
# Redirect everything through index.php
RewriteCond %{REQUEST_URI} !(/(api|edit)/?.*) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [NC,L,QSA]
# long list of 301 redirects
目前,我正在處理的三個重寫沒有任何作用。 如果我在正則表達式的開頭刪除斜杠,則會收到500 Internal Server Error。 任何想法可能出什么問題或如何解決?
交換以下三個規則:
# This is where I'm currently working
RewriteRule ^/([^/]+)$ /song-list?artist=$1 [NC,L,QSA]
RewriteRule ^/genre/([^/]+)$ /song-list?genre=$1 [NC,L,QSA]
RewriteRule ^/topic/([^/]+)$ /song-list?topic=$1 [NC,L,QSA]
對於這三個規則:
# If not /genre/* and not /topic/*
# Convert /dc-talk to /song-list?artist=dc-talk
RewriteCond %{REQUEST_URI} !^/genre/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/topic/(.*)$ [NC]
RewriteRule ^(.*)$ song-list?artist=$1 [NC,L,QSA]
# Convert /genre/rock to /song-list?genre=rock
RewriteRule ^genre/(.*)$ song-list?genre=$1 [NC,L,QSA]
# Convert /topic/forgivenessto /song-list?topic=forgiveness
RewriteRule ^topic/(.*)$ song-list?topic=$1 [NC,L,QSA]
您可以在此處查看以下規則: https : //htaccess.madewithlove.be/?share=763f7107-f056-5ebe-ae0a-7f53a17b225a
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.