簡體   English   中英

.htaccess 漂亮的 url 博客

[英].htaccess pretty url for blog

我需要有關博客的幫助,該博客位於核心 php 中,並希望使用 .htaccess 創建一個漂亮的 url

我有一個名為 blog 的文件夾,其中有兩個文件。

index.php 是一篇博客頁面文章。php 顯示一篇博客文章

所以我想要實現的是有人訪問

https://example.com/blog/

它顯示來自 index.php 的數據

在 post.php 我使用 url slug 不使用 id 獲取數據,所以如果有人寫

https://example.com/blog/my-blogpost-url-slug

It needs to call post.php which has logic to get url so it will be post.php?url=my-blogpost-url-slug will be converted to https://example.com/blog/my-blogpost-url-蛞蝓

基本上

https://example.com/blog/index.php should open like example.com/blog/ https://example.com/blog/post?url=mypost-url-slug should open like https://example. com/blog/mypost-url-slug

我嘗試了以下代碼,但它不起作用......

All index.php is being redirected to post.php and also url parmater is getting value post.php instead slug url

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

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

RewriteRule ^([\S\s/+.]+)/?$ post.php?url=$1 [NC,L]

對於您顯示的示例,請嘗試遵循 .htaccess 規則文件。

確保:

  • 將您的 .htaccess 規則文件與您的博客文件夾一起保存(不在其中,除此之外)。
  • 確保在測試 URL 之前清除瀏覽器緩存。
RewriteEngine ON
##Rules for redirecting FROM https://example.com/blog/index.php TO example.com/blog/
RewriteCond %{THE_REQUEST} \s/(blog)/index\.php\s [NC]
RewriteRule ^ /%1? [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/?$ blog/index.php [NC,L]

##Rules for redirecting FROM https://example.com/blog/post?url=mypost-url-slug TO https://example.com/blog/mypost-url-slug
RewriteCond %{THE_REQUEST} \s/(blog)/post\.php\?(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([^/]*)?$ blog/post.php?url=$1 [NC,L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM