递归地使用htaccess的手柄查询参数 [英] Handle query parameters recursively using htaccess

查看:111
本文介绍了递归地使用htaccess的手柄查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里再次重写URL的查询:(。
我使用htaccess的进行再创作,下面是我的htaccess code:

  RewriteEngine叙述上
的RewriteBase /

的RewriteCond%{ENV:REDIRECT_STATUS} 200
重写规则^  -  [L]

的RewriteCond%{REQUEST_URI}!-f
的RewriteCond%{REQUEST_URI}!\。\ w + $
的RewriteCond%{REQUEST_URI}!/ $
重写规则(。*)/ $ 1 / [R,L]

的RewriteCond%{REQUEST_URI} ^ /([\ W \ D  - ] +)/ $ [NC]
重写规则^ /?FILE_NAME =%1 [L,QSA]

的RewriteCond%{QUERY_STRING}!^ $
的RewriteCond%{QUERY_STRING}(?:(*)及)?FILE_NAME =([\ W \ D  - ] +)(。*)[NC]
重写规则^%2 /?%1%3 [L,R]
 

在问我的疑问让我给大家介绍了如何通过上述code正在简短的解释。我有一个像 http://www.mysite.com/ (索引)的等页面可以通过使用一个名为FILE_NAME = PAGE_NAME样的参数执行 http://www.mysite.com/?file_name=my_blog 。我的htaccess的帮助我排除file_name参数,并作出新的URL例如 http://www.mysite.com/my_blog/

现在我有一个查询:

  
      
  1. 右键现在已经有一个参数的 FILE_NAME 现在我想给喜欢的额外的参数http://www.mysite.com/?file_name=my_blog&blog_alias=welcome-to - 新的一代,并希望这个看起来像 http://www.mysite.com/my_blog/welcome-to-new-generation/ 。所以,如果我添加任何数量的参数有参数的名称应被删除,只有参数值带有斜杠。
      请注意:的参数名称可以是任何东西
  2. 。   

请请请帮助。

解决方案

这是一个非常有趣的问题,需要递归执行mod_rewrite规则。将这个code在你的.htaccess:

更新II 根据进一步的评论,重定向 / FILE_NAME = my_blog和放大器; blog_alias =欢迎到新一代的&放大器;富=酒吧和放大器; N = V 的URI / FILE_NAME + my_blog / blog_alias +欢迎到新一代/富+酒吧/ N + V

 选项+了FollowSymLinks -MultiViews
#开启mod_rewrite的上
RewriteEngine叙述上
的RewriteBase /

#转发FILE_NAME = my_blog和放大器; blog_alias =欢迎到新世代
#/ FILE_NAME = my_blog和放大器; blog_alias =欢迎到新一代
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ S / + \?([^ \ s] +)\ S [NC]
重写规则^ $%1? [L]

#重定向/ FILE_NAME = my_blog和放大器; blog_alias =欢迎到新一代的&放大器;富=酒吧和放大器; N = V
#到/ FILE_NAME + my_blog / blog_alias +欢迎到新一代/ FO +谷仓+ / V
重写规则^([^ =] +)=([^&功放;] +)(* /)?&安培; $ $ 1 $ 2 + $ 3 / $ 4 L](*)。
重写规则^(。* /)?([^ =] +)=(。*)$ $ 1 $ 2 + $ 3 [L,R]

#内部向前/ FILE_NAME + my_blog / blog_alias +欢迎到新世代
#/ FILE_NAME = my_blog和放大器; blog_alias =欢迎到新一代
重写规则^([^ \ +] +)\ +([^ /] +)/(。*)$ $ 3〜$ 1 = $ 2 [L,QSA]
重写规则^([^ \ +] +)\ +([^ /] +)/?$ /?$ 1 = $ 2 [L,QSA]
 

一旦你确定它正在改变研究 R = 301

I am here again with the query of re-writing URL :(.
I am using htaccess for re-writing, below is my htaccess code:

RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} !-f 
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$ 
RewriteRule (.*) /$1/ [R,L]

RewriteCond %{REQUEST_URI} ^/([\w\d-]+)/$ [NC]
RewriteRule ^ /?file_name=%1 [L,QSA]

RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^ %2/?%1%3 [L,R]

Before asking my queries let me give you short explanation about how above code is working. I have a URL like http://www.mysite.com/ (for index) and other pages could be executed by using a parameter called file_name=page_name like http://www.mysite.com/?file_name=my_blog. My htaccess is helping me to exclude the file_name parameter and make a new URL like http://www.mysite.com/my_blog/.

Now I do have a query:

  1. Right now there is already a parameter file_name and now I want to give extra parameters like http://www.mysite.com/?file_name=my_blog&blog_alias=welcome-to-new-generation and wanted this to look like http://www.mysite.com/my_blog/welcome-to-new-generation/. So if I add any number of parameter there parameter name should be removed and only parameter value comes with slash.
    Please note: Parameter name could be anything.

Please please please help.

解决方案

This is a very interesting problem that requires recursive implementation of mod_rewrite rules. Put this code in your .htaccess:

Update II As per further comments, redirects /?file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v URI to /file_name+my_blog/blog_alias+welcome-to-new-generation/foo+bar/n+v

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# forwards ?file_name=my_blog&blog_alias=welcome-to-new-generation to
# /file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?([^\s]+)\s [NC]
RewriteRule ^$ %1? [L]

# redirects /file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v
# to /file_name+my_blog/blog_alias+welcome-to-new-generation/fo+barn+/v    
RewriteRule ^(.*/)?([^=]+)=([^&]+)&(.*)$ $1$2+$3/$4 [L]
RewriteRule ^(.*/)?([^=]+)=(.*)$ $1$2+$3 [L,R]

# internal forward from /file_name+my_blog/blog_alias+welcome-to-new-generation to
# /?file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteRule ^([^\+]+)\+([^/]+)/(.*)$ $3?$1=$2 [L,QSA]
RewriteRule ^([^\+]+)\+([^/]+)/?$ /?$1=$2 [L,QSA]

Once you're sure it is working change R to R=301

这篇关于递归地使用htaccess的手柄查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆