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


問題描述

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

I'm doing my first steps with url‑rewriting and can't get the following to work:

In my application, a skin can be loaded by applying query parameter ?skin=some_id to any page in the application. I want to change:

 http://www.mysite.com/anypage.html?skin=123

into:

 http://www.mysite.com/123/anypage.html

but I cannot get it to work.

This is what I currently have in my httpd.conf:

<IfModule mod_rewrite.c>
    RRewriteRule (.*)/(.*)?app=(.*)$ %1/%3/%2 [NC,R=301,L]
</IfModule>

Questions: This isn't working, so I would like to know what I'm doing wrong? 

Also with the URL in effect, what is the URL the user enters? http://www.mysite.com/123/anypage.html which "maps" to http://www.mysite.com/anypage.html?skin=123

And if I want to access the query parameter, do I have to extract it from the actual url (?skin=...) or from the rewritten URL?

Thanks for helping out!

EDIT: So I have it sort of working doing it like this (helpful tester here):  

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} skin=(\w+)
RewriteRule ^.*\.html /%1? [R=301]
</IfModule>

This will redirect:

 www.some.com/index.html?skin=xyz   =>  www.some.com/xyz

Not quite there yet.


參考解法

方法 1:

I'd recommend going about skinning your application differently.  The way you have it now will create duplicate content issues with search engines because they will see the same content for each page on your site for every skin you have.

That is to say, yoursite.com/dark/about.html would be identical content to yoursite.com/spring/about.html so search engines may have a hard time deciding which version to use.  In addition, it seems like it will create extra work for linking to other pages on your site since you will have to create your links programmatically to use the proper path and skin.

I would just have a URL for activating a skin and store their preference in a cookie or in a session and skin the site based on the cookie/session value and only maintain one set of URLs.

Unless you really want the skin to be in the URL, I would shy away from using the URL or query string to indicate which skin to use.  Instead have it be a preference attached to an account or stored in a cookie.

(by frequentdrew010)

參考文件

  1. How to mod_rewrite a query parameter to a path folder name? (CC BY‑SA 3.0/4.0)

#url-rewriting #url #query-parameters #mod-rewrite #SEO






相關問題

靜態內容域的 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)







留言討論