rewrite rules for utm_source parameter in url - .htaccess

guys, i have question on mod_rewrite function.
this is outside link like
http://example.com/article?utm_sources=baidu&utm_campain=alading
I like to change it to
http://example.com/article#utm_sources=baidu&utm_campain=alading
#utm_sources=xxx also can be traced by Google GA code
I try below code but not working
RewriteCond %{QUERY_STRING} ^utm_source(&.*)?$ [NC]
RewriteRule ^article$ /article?%1 [R=301,NE,NC,L]
how do i make rewrite rules for it? thanks

You can use this rule:
RewriteCond %{QUERY_STRING} ^utm_sources=.+ [NC]
RewriteRule ^article/?$ /article#%{QUERY_STRING}? [R=301,NE,NC,L]
Trailing ? is required to strip off previous query string.

I update with below rules it works for me, but thanks anubhava give answer.
RewriteCond %{QUERY_STRING} ^utm_(.*)?$
RewriteRule ^(.*)$ %{REQUEST_URI}#%{QUERY_STRING}? [R=301,NE,NC,L]

Related

.htaccess Rewrite Query String

I have troubles redirecting query strings and I really can't find a way how to do it. Let me be more specific:
I want to achieve the following: Redirect http://blablabla.com/default.asp?Kategori=1&Valg=77 to: http://blablabla.com/default_Kategori_1_Valg_77.html
Any help will be very appreciated.
Kind Regards,
Pavel Nikolaev
This should work :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Kategori=1&Valg=77$ [NC]
RewriteRule ^default\.asp$ http://blablabla.com/default_Kategori_1_Valg_77.html? [L,R]
I would like to provide a new version of what starkeen advised me:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^Kategori=(.*)&Valg=(.*)$ [NC]
RewriteRule ^default\.asp$ http://blablabla.com/default_Kategori_%1_Valg_%2.html? [L,R]
The difference is that know I am catching the values of the Kategori and Valg parameters and use them in the new URL. Really interesting!
Thank you all.

URL rewriting via my .htaccess file

How do I change example.com/1 to example.com/?id=1
I've tried googling but I can only find code for example.com/?id=1 to example.com/1
I used a generator and got this, but it didn't work
RewriteEngine On
RewriteRule ^\?get\=$ / [L]
Thanks,
Isaac
It kind of depends on which way you want to do the rewrite - ie, what does the user type see, and what does the server do.
If you want the user to see "http://example.com/1" and internally the server provides "http://example.com/?id=1", then the following should work:
RewriteRule ^([0-9]+)$ /?id=$1
However, if you want the user see "http://example.com/?id=1", and internally the server provides "http://example.com/1", then the following, as per Jon Lin's answer, should do it:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1?
You can't match against the query string in a RewriteRule statement, you need to use a RewriteCond and the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1? [L]
The ? is needed in the rule's target to remove the query string.

Specific .htaccess redirect based on query_string

I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

how to redirect from modules.php?name=News&file=article&sid=XXX to domain.eu/XXX?

I would like to redirect all url requests with the phpnuke string modules.php?name=News&file=article&sid=XXX (xxx is a number) to my domain-name.eu/XXX . How is that possible?
i tried
RewriteCond %{QUERY_STRING} ^(.*&|)modules.php?name=News&file=article&sid=(&.*|)$
RewriteRule ^(.*)$ %1domain.eu/%2 [L,R=301]
but it has no effect. Where is the mistake?
Also i don't understand why a simple
RewriteRule ^(.*)$ /modules.php?name=News&file=article&sid=$1 [L,R=301]
doenst work. Any ideas?
Thanks a lot for any help.
Not sure why I'm seeing this in Drupal questions. But try this.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/modules\.php$
RewriteCond %{QUERY_STRING} ^sid=([0-9]*)$
RewriteRule ^(.*)$ http://domain.eu/%1 [R=301,L]
You can't match everything after the sid=(.*) regex part in the URL in most cases. modules.php?name=News&file=article&sid=XXX and modules.php?sid=XXX&name=News&file=article are the same in functionality.
Learned at http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/

Help with .htaccess RewriteRule. Need to take user from one URL to other

I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other

Resources