如何使用 .htaccess 將動態 URL 更改為 SEO 友好 (how to change the dynamic URL to SEO friendly using .htaccess)


問題描述

如何使用 .htaccess 將動態 URL 更改為 SEO 友好 (how to change the dynamic URL to SEO friendly using .htaccess)

I want to change the dynamic URL www.example.com/business‑listing.php?id=7863 to www.example.com/some‑dynamic‑text/7863.

Also I want to hide .php extension from all URL, for eg: the URL www.example.com/list/page.php  should be change to www.example.com/list/page

And also when an user enter the URL www.example.com/list/page.php, he should restricted to access that page using that URL. Permit acess only using 

www.example.com/list/page

I have tried the following .htaccess file:

DirectoryIndex home.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^home$ home.php
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^([a‑zA‑Z0‑9_,‑]+)/([a‑zA‑Z0‑9_,‑]+)$ business‑listing.php?id=$1
  1. This take the first regular expression as value of id, but I want the Second.
  2. This hide the php extension of only home.php, but I want all php files.
  3. This does not  prevent the user when entering the URL with PHP extension, That is when an user enter the URL www.example.com/bill/pay.php the page is open, but I want to restrict it.

參考解法

方法 1:

Try this code:

RewriteRule ^$ ‑ [L]

RewriteCond $1 ^(*\.php)
RewriteRule ^(.*)$ ‑ [F,L]

RewriteRule ^/?([^\./]*)[:;,\.]*$ $1.php

RewriteRule ^list/(.*)$ business‑listing.php?id=$1 

If you want the 'list' part of the URL dynamic, try:

RewriteRule ^(.*)/(.*)$ business‑listing.php?id=$2

方法 2:

RewriteRule ^(.*)/(.*)$ business‑listing.php?id=$2&second=$2 [R,NC,L]

Try this!

方法 3:

You should not do it using .htaccess. Migration to nginx will be very painful in this case. Also you should remember of Front Controller Pattern.

URL should be processed by PHP(python, ryby etc...). There is variety of ready frameworks for url routing and a lot of cool things, like database api or template engine. In the php‑world there are a lot of variants.As for me, I prefer Silex microframework and Symfony2. Yii and Zend2 are worth seeing too.

For example, in silex you can bind a function to url‑pattern in such a nice way:

// web/index.php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app‑>get('/hello/{name}', function ($name) use ($app) {
    return 'Hello '.$app‑>escape($name);
});

$app‑>run();

Good luck.

(by HYDER ALIPieterpremananthAlexey Sidash)

參考文件

  1. how to change the dynamic URL to SEO friendly using .htaccess (CC BY‑SA 3.0/4.0)

#url-rewriting #.htaccess #PHP






相關問題

靜態內容域的 IIS 重定向規則 (IIS redirect rules for static content domains)

IIS 重寫模塊重寫映射性能 (IIS Rewrite Module rewrite map performance)

我返回集合的 Asp.net mvc 操作生成包含 system.linq.Enumerable 的 url,如何刪除它們 (My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them)

如何將查詢參數修改為路徑文件夾名稱? (How to mod_rewrite a query parameter to a path folder name?)

如何使用 .htaccess 將動態 URL 更改為 SEO 友好 (how to change the dynamic URL to SEO friendly using .htaccess)

AngularJS 和 URL 重寫為 HTTPS (AngularJS and URL Rewrite to HTTPS)

mod-rewrite 結合了兩種不同的重寫規則 (mod-rewrite combine two different rewrite rules)

htaccess 刪除 .php 擴展名並重寫為 www (htaccess remove .php extension and rewrite to www)

初學者 Apache URL 重寫問題 (Beginner Apache URL Rewrite Question)

.net datapager + listview + Intelligencia.UrlRewriter (.net datapager + listview + Intelligencia.UrlRewriter)

jquery get / post請求的Apache url替換 (Apache url replacement for jquery get / post request)

在python中解析url以提取參數 (Parsing url in python to extract parameters)







留言討論