Alex
Alex

Reputation: 540

How to REGEX and .htaccess rewrite url

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

Upvotes: 4

Views: 45582

Answers (2)

Sam
Sam

Reputation: 5536

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.

Upvotes: 1

dabito
dabito

Reputation: 586

I think you're missing the $ end tag for your Regular Expression. Try the next line instead:

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

also, check this article for additional rules and options:

http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Upvotes: 0

Related Questions