GET-Request 中的斜杠 (/) 和我在 htaccess 中的 mod_rewrite 语句
slash (/) in GET-Request and my mod_rewrite statement in htaccess
我对这个话题有点疯狂,希望能得到一些帮助。
我目前正在将我的网站重建为 MVC 结构。这也包括 SEO 友好(漂亮)URLs.
我已经实现了 URL-请求
的转变
from: http://www.example.com/company?id=about_us
to: http://www.example.com/company/about_us
我的 .htaccess 文件
RewriteEngine On
RewriteBase /
# Transforms an ugly-URL into a pretty-URL ('external redirect' updates also adress in browser)
# ugly URL: www.example.com/company?id=about_us
# pretty URL: www.example.com/company/about_us
RewriteCond %{QUERY_STRING} ^id=([\w-]+)$
RewriteRule ^(.+)$ /%1? [R=301,L]
# Transform an pretty-URL into a ugly-URL ('internal redirect')
# pretty URL: www.example.com/company/about_us
# ugly URL: www.example.com/index.php?url=company/about_us
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [QSA,L]
现在 GET-Request of a FORM (select box for some articles) 进入游戏,它不能与上述 htaccess 文件一起使用。而每篇文章都有一个 SEO_slug 保存在数据库中,动态放入表单中。 SEO-slug 已经有了这种格式:
"<city>/<type>/<articlename>"
HTML 看起来像这样:
<form method='get' action='../articles/'>
<select name='id'>
<option value='london/fruit/article_1' >Article 1</option>
<option value='london/nuts/article_2' >Article 2</option>
<option value='newyork/fruit/article_3'>Article 3</option>
<option value='newyork/nuts/article_4' >Article 4</option>
<option value='miami/fruits/article_5' >Article 5</option>
</select>
</form>
问题:
现在,请求已发送到服务器,但斜杠 (/) 已转换为“%2f”,这会在我当前的 htaccess 中生成服务器内部错误。
问题
1) 我可以阻止从斜杠 (/) 到 '%2f' 的转换吗?
2) 我该如何更新我的 mod_rewrite 才能启用此功能。看了那么多网站,一直没找到好的解决办法。我能够用这个实现一点:
RewriteCond %{QUERY_STRING} ^id=([\w-]+)(%2F*)(.*)(%2F*)(.*)$
RewriteRule ^(.+)$ /%1/%3/%5? [R=301,L]
,但我对斜线的数量有疑问,因为有时深度不同。
谁能给我一个好的建议?非常感谢!
也许我试图以错误的方式解决这个故事,需要完全不同的想法??
干杯
蒂姆
终于找到了解决方法,分享给大家:
RewriteEngine On
RewriteBase /
# Transforms an ugly-URL into a pretty-URL ('external redirect' updates also adress in browser)
# ugly URL: www.example.com/company?id=about_us
# pretty URL: www.example.com/company/about_us
#
# REMARK: The important group is the thrid. The two first groups are
# important if the query string contains slahes (/) that are
# encoded as '%2F'. As there is no 'replace'-function each
# Level of '%2F' needs to be rewritten step by step
RewriteCond %{QUERY_STRING} ^id=([\w-]+)%2F([\w-]+)%2F([\w-]+)$
RewriteRule ^(.+)$ /%1/%2/%3? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=([\w-]+)%2F([\w-]+)$
RewriteRule ^(.+)$ /%1/%2? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=([\w-]+)$
RewriteRule ^(.+)$ /%1? [R=301,L]
# Transform an pretty-URL into a ugly-URL ('internal redirect')
# pretty URL: www.example.com/company/about_us
# ugly URL: www.example.com/index.php?url=company/about_us
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [QSA,L]
如果您对此有任何改进意见,我很高兴听到。
我对这个话题有点疯狂,希望能得到一些帮助。
我目前正在将我的网站重建为 MVC 结构。这也包括 SEO 友好(漂亮)URLs.
我已经实现了 URL-请求
的转变from: http://www.example.com/company?id=about_us
to: http://www.example.com/company/about_us
我的 .htaccess 文件
RewriteEngine On
RewriteBase /
# Transforms an ugly-URL into a pretty-URL ('external redirect' updates also adress in browser)
# ugly URL: www.example.com/company?id=about_us
# pretty URL: www.example.com/company/about_us
RewriteCond %{QUERY_STRING} ^id=([\w-]+)$
RewriteRule ^(.+)$ /%1? [R=301,L]
# Transform an pretty-URL into a ugly-URL ('internal redirect')
# pretty URL: www.example.com/company/about_us
# ugly URL: www.example.com/index.php?url=company/about_us
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [QSA,L]
现在 GET-Request of a FORM (select box for some articles) 进入游戏,它不能与上述 htaccess 文件一起使用。而每篇文章都有一个 SEO_slug 保存在数据库中,动态放入表单中。 SEO-slug 已经有了这种格式:
"<city>/<type>/<articlename>"
HTML 看起来像这样:
<form method='get' action='../articles/'>
<select name='id'>
<option value='london/fruit/article_1' >Article 1</option>
<option value='london/nuts/article_2' >Article 2</option>
<option value='newyork/fruit/article_3'>Article 3</option>
<option value='newyork/nuts/article_4' >Article 4</option>
<option value='miami/fruits/article_5' >Article 5</option>
</select>
</form>
问题:
现在,请求已发送到服务器,但斜杠 (/) 已转换为“%2f”,这会在我当前的 htaccess 中生成服务器内部错误。
问题
1) 我可以阻止从斜杠 (/) 到 '%2f' 的转换吗?
2) 我该如何更新我的 mod_rewrite 才能启用此功能。看了那么多网站,一直没找到好的解决办法。我能够用这个实现一点:
RewriteCond %{QUERY_STRING} ^id=([\w-]+)(%2F*)(.*)(%2F*)(.*)$
RewriteRule ^(.+)$ /%1/%3/%5? [R=301,L]
,但我对斜线的数量有疑问,因为有时深度不同。
谁能给我一个好的建议?非常感谢! 也许我试图以错误的方式解决这个故事,需要完全不同的想法??
干杯 蒂姆
终于找到了解决方法,分享给大家:
RewriteEngine On
RewriteBase /
# Transforms an ugly-URL into a pretty-URL ('external redirect' updates also adress in browser)
# ugly URL: www.example.com/company?id=about_us
# pretty URL: www.example.com/company/about_us
#
# REMARK: The important group is the thrid. The two first groups are
# important if the query string contains slahes (/) that are
# encoded as '%2F'. As there is no 'replace'-function each
# Level of '%2F' needs to be rewritten step by step
RewriteCond %{QUERY_STRING} ^id=([\w-]+)%2F([\w-]+)%2F([\w-]+)$
RewriteRule ^(.+)$ /%1/%2/%3? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=([\w-]+)%2F([\w-]+)$
RewriteRule ^(.+)$ /%1/%2? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=([\w-]+)$
RewriteRule ^(.+)$ /%1? [R=301,L]
# Transform an pretty-URL into a ugly-URL ('internal redirect')
# pretty URL: www.example.com/company/about_us
# ugly URL: www.example.com/index.php?url=company/about_us
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url= [QSA,L]
如果您对此有任何改进意见,我很高兴听到。