簡體   English   中英

如何在 PHP Codeigniter 中重寫 URL

[英]How to rewrite a URL in PHP Codeigniter

你好,我是 CI 的新手,我被困在了某個地方。

我想要的是我有這樣的網址

domain.com/article?seo=What-Software-Program&id=7

在這里,我通過最后的 id 獲取文章,我正在使用 seo 在 url 中使用與 thsse 相關的關鍵字以獲得更好的 SEO

現在我想要從那個 url 看起來像這樣

domain.com/article/What-Software-Program/id/7

我想從 url 中刪除?seo=並將其替換為 /

我已經成功地配置了我的路由頁面來路由沒有 index.php 和主控制器的 url

這是我的路線頁面

$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = "main/$1";
$route['admin'] = 'admin/login';

和我的 htaccess

RewriteEngine On

# remove /index.php/main/ from URLs
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+index\.php/main/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^index\.php/main(/.*)?$ https://%1$1 [L,NC,NE,R=301]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    Header set Access-Control-Allow-Origin "*"
</IfModule>

# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Header set Access-Control-Allow-Origin "*"

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

編輯

我有這樣的網址 domain.com/article?seo=What-Software-Program&id=7 但是這個網址是在我刪除我的默認控制器名稱和 index.php 之后

原始 url 是 domain.com/index.php/main/article?seo=What-Software-Program&id=7 其中 main 是控制器名稱 article 是函數名稱

我通過 route.php 文件刪除了 index.php 和 main,現在我想將 SEO 的內容正確地放在 url 中。

For this type of url : domain.com/article/What-Software-Program/id/7

請在routes.php文件中添加以下路由。

$route['article/(:any)/id/(:num)'] = "controller_name/function_name/$1/$2";

暫無
暫無

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

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