mod_rewrite: rewrite specific URL - apache

I want to rewrite a specific URL, I'll show an example so you'll understand what I mean.
First, my current rewrite rule:
RewriteRule ^/?([a-zA-Z0-9/-]+)/?$ /index.php [NC,L]
Now I want this URL:
http://example.tld/foobar?test
Rewritten to:
http://example.tld/foobar
Note: only for /foobar?test! E.g. not for /somethingelse?test and also not for /foobar?blah!
Thanks in advance!
EDIT: I realized I want a 301 redirect from /foobar?test to /foobar, not a "traditional" rewrite. Hope that is possible.

RewriteCond %{QUERY_STRING} ^test$
RewriteRule ^/foobar$ /foobar [NC,R=301,L]

Related

Rewriting part of url using .htaccess

I want to rename the following urls
http://xyz.website.com/search/sOrder,dt_pub_date/iOrderType,desc
to
http://xyz.website.com/search/sOrder,dt_pub_date/iOrderType,desc/category,111
and
http://xyz.website.com/index.php?page=search<&item=1&city=2...and so on>
to
http://xyz.website.com/index.php?page=search&sCategory=111<&item=1&city=2...and so on>
for the first redirect, this an quick solution(if the query is static):
RedirectMatch permanent ^/(search/sOrder,dt_pub_date/iOrderType,desc).*$ /search/sOrder,dt_pub_date/iOrderType,desc/category,111
For the second one:
RewriteCond %{QUERY_STRING} ^page=search(.*)$
RewriteRule ^index.php$ /index.php?sCategory=111%1 [R=301,L]
Hope this will helps you.

Rewrite rule always redirect to wrong url

Hello All,
I need to rewrite http://mysite.com/user/profile/following?profile_name=MYNAME to http://mysite.com/user/profile/MYNAME/following
I have written rule like this:
RewriteRule user/profile/(.*)/(.*) /user/profile/$2?profile_name=$1 [L,R=301]
when i put url like http://mysite.com/user/profile/MYNAME/following in the browser it always redirect me to http://mysite.com/user/profile/following?profile_name=MYNAME
What did i miss?
Thanks in Advance
You can use this code:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+user/profile/([^?]+)\?profile_name=([^\s&]+) [NC]
RewriteRule ^ /user/profile/%1/%2? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^user/profile/([^/]+)/([^/]*)/?$ /user/profile/$2?profile_name=$1 [L,NC,QSA]

Apache Rewrite problem

http://www.a.com/content/1
http://www.a.com/content/seo-text-1
RewriteRule ^content/(\d+)|.+?-(\d+) action/index.php?id=$2
This rule don't work second url, How to fix it?
Thank you
If for any reason you need to have it in single rule, you can use this:
RewriteRule ^content/(.*)-?(\d+)$ action/index.php?id=$2 [R]
But more readable solution is to use two rules:
RewriteRule ^content/(\d+)$ action/index.php?id=$1 [R]
RewriteRule ^content/.*-(\d+)$ action/index.php?id=$1 [R]

htaccess rewrite

I've rebuilt a site using a CMS and I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: ?secc=country_club. For instance, domain.com?secc=country_club.
I would like to either have a rule for each url or have it rewrite the ?secc=country-club to just country-club
This is what I have tried, without any success:
RewriteRule ^secc-([^-]*)$ /?secc=$1 [L]
I think it has something to do with the ? in the url
Also if it helps, I am using joomla and I do have sh404sef.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^secc=(.+)$
RewriteRule ^(.*) %1? [R,L]
This will redirect http://example.com/?secc=MYPAGE to http://example.com/MYPAGE
I think you meant to write '=' after ^secc:
RewriteEngine on
RewriteRule ^?secc=(.*)$ "$1" [QSA]

Apache rewrite rule with parameters?

I have the following URL:
http://domain.com/index.php?m=feedback&cSubject=My Subject
I want to have a rewrite rule so that the following:
http://domain.com/feedback?Subject=My Subject
maps to the previous url. Heres my rule at the moment:
RewriteRule ^feedback?Subject=(.*)$ index.php?m=feedback&cSubject=$1
Doesn't seem to be working tho! Any ideas?
Query Strings are not parsed by Apache Mod_Rewrite, but there is a workaround. Try this
RewriteRule ^feedback/?$ index.php?m=feedback&c%{QUERY_STRING} [NC,L]
You can use RewriteCond statement to do exactly what you want:
RewriteEngine On
RewriteCond %{QUERY_STRING} Subject=(.*)
RewriteRule ^feedback$ index.php?m=feedback&cSubject=%1 [L]
There seems to be an = missing from clops answer to give..
RewriteRule ^feedback/?$ index.php?m=feedback&c=%{QUERY_STRING} [NC,L]
.. at least I need one to make it work.