Forum Moderators: phranque
Removing the Query String
RewriteRule ^/url /url?
Adding to the Query String
Keep the existing query string using the Query String Append flag, but add var=val to the end.
RewriteRule ^/url /url?var=val [QSA]
Rewriting For Certain Query Strings
Rewrite URLs like http://askapache.com/url1?var=val to http://askapache.com/url2?var=val but don't rewrite if val isn't present.
RewriteCond %{QUERY_STRING} val
RewriteRule ^/url1 /url2
Modifying the Query String
Change any single instance of val in the query string to other_val when accessing /path. Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond.
RewriteCond %{QUERY_STRING} ^(.*)val(.*)$
RewriteRule /path /path?%1other_val%2
can I rewrite part of a URL in htaccess ...
If it was this one url I could 301 it
mywebsite . com /old/component /tag/colorado-sunday-meetings
to
mywebsite . com /new/component /tag/colorado-sunday-meetings
Query String
RewriteRule ^/urlThe subject line of this thread says explicitly “htaccess”. A leading slash / is used only when a RewriteRule is lying loose in config (most likely in a VirtualHost envelope). In any directory context, including htaccess, you omit the leading / slash.
example.com/old/component/tag/pagenameto
example.com/new/component/tag/pagenameand you are using your .htaccess file (rather than a .conf configuration file) to do that. Is this correct so far?
example.com/old/component/tag/pagenameto see
example.com/new/component/tag/pagenameinstead. Is this correct?
/component/tag/pagenamefrom the old URL to add it back on after the /new/ change?
my hosting gives me cPanel accessI should hope so. But better yet is to open the htaccess file itself in a text editor, edit, and upload the changed version. Save a copy locally as something like “htaccess_mysite” without leading . dot, and give it its correct name when uploading. (You can edit the htaccess on your server directly, but it isn't advisable unless it’s a very simple edit.)
I only need this:Well, that sounds like a single conditionless RewriteRule. You could even do it in mod_alias (RedirectMatch) if you don’t mind the occasional double redirect.