如何正则表达式和.htaccess重写URL重写、正则表达式、htaccess、URL

2023-09-02 00:33:51 作者:初冬丶浅凉

我有这个网址,我需要抓住的标签和重写URL

http://beantownbride.com/darkroom/mt/mt-search.cgi?blog_id=1&tag=Social%20Media&limit=20&IncludeBlogs=1

因此​​,在上面的东西更是这样 http://beantownbride.com/?s =社交+媒体

下面是我到目前为止我的.htaccess文件

 重写规则^ HTTP://beantownbride.com/darkroom/mt/mt-search.cgi blog_id =(*)及标签=(*)的http:// beantownbride.com/?s=$2
 

但它仅适用于第一个标签 (如果它在所有工作)

易语言正则表达式

全.htaccess文件

  RewriteEngine叙述上
#BEGIN字preSS



#MT到WP
#转换旧档案
重写规则^ /归档/([0-9] *)。PHP $ / 1 $ / [R = 301,L]
重写规则^ /归档/([0-9] *)。HTML / $ 1 / [R = 301,L]
重写规则^ /归档/([0-9] *)的shtml  $ / $ 1 / [R = 301,L]
重写规则^ /归档/([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9] /.*) PHP $ / 1 $ / [R = 301,L]
重写规则^ /档案馆/老/([0-9] *)。PHP $ / 1 $ / [R = 301,L]
重写规则^ /档案馆/猫_(。+)。PHP $ /分类/ 1 $ / [R = 301,L]
重写规则^ /档案馆/猫_(。+)。HTML $ /分类/ 1 $ / [R = 301,L]
重写规则^ /档案馆/猫_(。+)。SHTML $ /分类/ 1 $ / [R = 301,L]
重写规则^ /档案馆/?$ /存档/ [R = 301,L]
的RewriteCond%{REQUEST_URI}!^ /档案馆/ [0-9] [0-9] [0-9] [0-9] / [0-9] [0-9] /?.*$ [NC]
的RewriteCond%{REQUEST_URI}!^ /档案馆/ [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]  *。$?[NC]
的RewriteCond%{REQUEST_URI}!^ /存档/标签/.*$ [NC]
的RewriteCond%{REQUEST_URI}!^ /档案馆/作者/.*$ [NC]
重写规则^ /档案馆/(.*)$ /分类/ $ 1 [R = 301,L]
重写规则^ /档案馆/(.*)$ / $ 1 [R = 301,L]

#更改下划线连字符。
重写规则^ /类/([^_]*)_([^_]*)_([^_]*)_(.*)$ /分类/ $ 1- $ 2- $ 3 $ 4 / [R = 301, L]
重写规则^ /类/([^_]*)_([^_]*)_(.*)$ /分类/ $ 1- $ 2- $ 3 / [R = 301,L]
重写规则^ /类/([^_]*)_(.*)$ /分类/ $ 1- $ 2 / [R = 301,L]
重写规则^ /([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9])/([^ _ /] *)_([^ _ /]*)_([^_/]*)_(.*)/(.+)$ / $ 1 / $ 2- $ 3- $ 4- $ 5 / $ 6 R = 301,L]
重写规则^ /([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9])/([^ _ /] *)_([^ _ /]*)_(.*)/(.+)$ / $ 1 / $ 2- $ 3- $ 4 / $ 5 R = 301,L]
重写规则^ /([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9])/([^ _ /] *)_(。*) /(.+)$ / $ 1 / $ 2- $ 3 / $ 4 R = 301,L]

#更改为字preSS供稿
重写规则^ /原子$ /wp-atom.php [R = 301,L]
重写规则^ /原子的.xml $ /wp-atom.php [R = 301,L]
重写规则^ /指数 .atom $ /wp-atom.php [R = 301,L]
重写规则^ /指数 .RDF $ /wp-rdf.php [R = 301,L]
重写规则^ /指数的.xml $ /wp-rss2.php [R = 301,L]
重写规则^ / RSS 的.xml $ /wp-rss.php [R = 301,L]
重定向永久/rss.xml /饲料/

重写规则^ /([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9] /.*)。原子/ $ 1 /料/ [R = 301,L]
重写规则^ /([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9] /.*)。原子/ $ / $ 1 /饲料/ [ R = 301,L]
重写规则^ /([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9] /.*)。XML / $ 1 /料/ [R = 301,L]
重写规则^ /([0-9] [0-9] [0-9] [0-9] / [0-9] [0-9] /.*)。XML / $ / $ 1 /饲料/ [ R = 301,L]

重定向永久/about.html /约
重定向永久/advertise.html /做广告
重定向永久/contact.html /接触
永久重定向/欢迎-约/ /约/
重定向永久/welcome-about/welcome-to-my-blog.html /约/
永久重定向/实时婚礼/真实婚礼

重写规则^ /事件/(.*)$ /?S =事件

#http://beantownbride.com/darkroom/mt/mt-search.cgi?blog_id=1&tag=Social%20Media&limit=20&IncludeBlogs=1
#RewriteRule ^ /暗室/ MT / MT-search.cgi blog_id = 1&安培;标签=(。*)及限制
#http://beantownbride.com/ S =测试+标签+胡说

#RewriteRule ^的http:(。*);(。*)//beantownbride.com/darkroom/mt/mt-search.cgi blog_id =安培标记= $ http://beantownbride.com/?s=$2
重写规则^ HTTP://beantownbride.com/darkroom/mt/mt-search.cgi blog_id =安培;标签=(。*)$ http://beantownbride.com/?s=$2?(*)。

< IfModule mod_rewrite.c>
的RewriteBase /
重写规则^指数 .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php [L]
< / IfModule>

#结束字preSS
 

解决方案

我知道这是一个老问题,但我对那些仍然在寻找一个答案的答案。

您需要在规则之前在条件查询字符串相匹配。

IE浏览器:

 的RewriteCond%{QUERY_STRING} ^ blog_id =&AMP(*)(。*)标记= $
重写规则^ HTTP://beantownbride.com/darkroom/mt/mt-search.cgi$ http://beantownbride.com/?s=%2
 

%2 在规则中的条件,第二回的参考。

I have this url and I need to grab the tags and rewrite the url

http://beantownbride.com/darkroom/mt/mt-search.cgi?blog_id=1&tag=Social%20Media&limit=20&IncludeBlogs=1

So, in the example above to something more like this http://beantownbride.com/?s=Social+Media

Here is what I have so far for my .htaccess file

RewriteRule ^http://beantownbride.com/darkroom/mt/mt-search.cgi?blog_id=(*.)&tag=(*.) http://beantownbride.com/?s=$2

but it only works for the first tag (if it works at all)

the whole .htaccess file

RewriteEngine On
# BEGIN WordPress



#MT to WP
# Convert old archives
RewriteRule ^/archives/([0-9]*).php$ /$1/ [R=301,L]
RewriteRule ^/archives/([0-9]*).html$ /$1/ [R=301,L]
RewriteRule ^/archives/([0-9]*).shtml$ /$1/ [R=301,L]
RewriteRule ^/archives/([0-9][0-9][0-9][0-9]/[0-9][0-9]/.*).php$ /$1/ [R=301,L]
RewriteRule ^/archives/old/([0-9]*).php$ /$1/ [R=301,L]
RewriteRule ^/archives/cat_(.+).php$ /category/$1/ [R=301,L]
RewriteRule ^/archives/cat_(.+).html$ /category/$1/ [R=301,L]
RewriteRule ^/archives/cat_(.+).shtml$ /category/$1/ [R=301,L]
RewriteRule ^/archives/?$ /archive/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/archives/[0-9][0-9][0-9][0-9]/[0-9][0-9]/?.*$ [NC]
RewriteCond %{REQUEST_URI} !^/archives/[0-9][0-9][0-9][0-9][0-9][0-9].?.*$ [NC]
RewriteCond %{REQUEST_URI} !^/archives/tag/.*$ [NC]
RewriteCond %{REQUEST_URI} !^/archives/author/.*$ [NC]
RewriteRule ^/archives/(.*)$ /category/$1 [R=301,L]
RewriteRule ^/archives/(.*)$ /$1 [R=301,L]

# Change underscores hyphens
RewriteRule ^/category/([^_]*)_([^_]*)_([^_]*)_(.*)$ /category/$1-$2-$3-$4/ [R=301,L]
RewriteRule ^/category/([^_]*)_([^_]*)_(.*)$ /category/$1-$2-$3/ [R=301,L]
RewriteRule ^/category/([^_]*)_(.*)$ /category/$1-$2/ [R=301,L]
RewriteRule ^/([0-9][0-9][0-9][0-9]/[0-9][0-9])/([^_/]*)_([^_/]*)_([^_/]*)_(.*)/(.+)$ /$1/$2-$3-$4-$5/$6 [R=301,L]
RewriteRule ^/([0-9][0-9][0-9][0-9]/[0-9][0-9])/([^_/]*)_([^_/]*)_(.*)/(.+)$ /$1/$2-$3-$4/$5 [R=301,L]
RewriteRule ^/([0-9][0-9][0-9][0-9]/[0-9][0-9])/([^_/]*)_(.*)/(.+)$ /$1/$2-$3/$4 [R=301,L]

# Change to wordpress feeds
RewriteRule ^/atom$ /wp-atom.php [R=301,L]
RewriteRule ^/atom.xml$ /wp-atom.php [R=301,L]
RewriteRule ^/index.atom$ /wp-atom.php [R=301,L]
RewriteRule ^/index.rdf$ /wp-rdf.php [R=301,L]
RewriteRule ^/index.xml$ /wp-rss2.php [R=301,L]
RewriteRule ^/rss.xml$ /wp-rss.php [R=301,L]
Redirect permanent /rss.xml /feed/

RewriteRule ^/([0-9][0-9][0-9][0-9]/[0-9][0-9]/.*).atom$ /$1/feed/ [R=301,L]
RewriteRule ^/([0-9][0-9][0-9][0-9]/[0-9][0-9]/.*).atom/$ /$1/feed/ [R=301,L]
RewriteRule ^/([0-9][0-9][0-9][0-9]/[0-9][0-9]/.*).xml$ /$1/feed/ [R=301,L]
RewriteRule ^/([0-9][0-9][0-9][0-9]/[0-9][0-9]/.*).xml/$ /$1/feed/ [R=301,L]

Redirect permanent /about.html /about
Redirect permanent /advertise.html /advertise
Redirect permanent /contact.html /contact
Redirect permanent /welcome-about/ /about/
Redirect permanent /welcome-about/welcome-to-my-blog.html /about/
Redirect permanent /real-wedding /real-weddings

RewriteRule ^/events/(.*)$ /?s=events

#http://beantownbride.com/darkroom/mt/mt-search.cgi?blog_id=1&tag=Social%20Media&limit=20&IncludeBlogs=1
#RewriteRule ^/darkroom/mt/mt-search.cgi?blog_id=1&tag=(*.)&limit
#http://beantownbride.com/?s=test+tag+blah

#RewriteRule ^http://beantownbride.com/darkroom/mt/mt-search.cgi?blog_id=(.*)&tag=(.*)$ http://beantownbride.com/?s=$2
RewriteRule ^http://beantownbride.com/darkroom/mt/mt-search.cgi?blog_id=(.*)&tag=(.*)$ http://beantownbride.com/?s=$2

<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

解决方案

I know this is an old question, but I have an answer for those still looking for an answer.

You need to match the query string in a condition before the rule.

IE:

RewriteCond %{QUERY_STRING} ^blog_id=(.*)&tag=(.*)$
RewriteRule ^http://beantownbride.com/darkroom/mt/mt-search.cgi$ http://beantownbride.com/?s=%2

The %2 in the rule is the second back reference in the condition.

 
精彩推荐
图片推荐