.htaccess rewrite rule for redirects - apache

I've been fighting this for a while and can't seem to make it work. My old system used a lot of query strings to render pages but they are no longer necessary. My url is below:
OLD URL: www.example.com/links.php?section=5&catid=52
NEW URL: www.example.com/mhfs/links
The name links is coincidental and not necessarily from the old pages name. I need to check which section and catid is present and redirect them to the appropriate page from what it is. I tried the following but this just seems to do nothing. What am I doing wrong?
RewriteCond %{REQUEST_URI} ^links.php$
RewriteCond %{QUERY_STRING} ^section=5$
RewriteCond %{QUERY_STRING} ^catid=52$
RewriteRule ^(.*)$ /mhfs/links? [R=301]
Any help would be GREATLY appreciated.

You must turn the rewrite engine on for it to work.
You probably don't want the regex start symbol on the replacement:
RewriteEngine on
RewriteCond ...
RewriteRule ...

Related

Modify query string value using htacces

I want to redirect
https://www.example.com/signup?plan=basic to https://www.example.com/signup?plan=basic-monthly and https://www.example.com/signup?plan=pro to https://www.example.com/signup?plan=pro-monthly .
How can I achieve this using htaccess ?
There are many questions related to this here. But, couldn't find an answer for this specific scenario.
This is the code I tried and failed:
RewriteCond %{QUERY_STRING} (^|&)plan=pro(&|$)
RewriteRule ^signup /$0?plan=pro-monthly [R=301,L]
Also, while trying the same with "basic" instead of "pro", the word "basic" i shown in red color as if it is a keyword.
Could you please try following, written with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(plan=(?:basic|pro))$ [NC]
RewriteRule ^(signup)/?$ $1?%1-monthly [L]
2nd solution: Or you could try following too. Make sure you either put 1st solution rules OR this one at a time.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(signup)\?(plan=(?:basic|pro))\s [NC]
RewriteRule ^ %1?%2-monthly [L]

Apache .htaccess rewrite parameter to directory

I've got an application that has been migrated to a newer platform. The tasks are similar and I'd like to redirect a GET parameter to a directory. For example
http://gallery/index.php?gal=ABC => http://photos/gal/ABC
and
http://gallery/?gal=DEF => http://photos/gal/DEF
and the anything that doesn't get caught redirect it to http://photos
I've tried
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^(/index.php)$ /%1/%2?
However all I get is a 404 and no redirection. Similarly, I've tried
RedirectMatch ^/index\.php\?=gal(.*) http://photos/gal/$1
but I'm having trouble escaping the ? in the original URL.
What would be the proper way of going about this?
Create a .htaccess file and insert the following code:
RewriteCond %{QUERY_STRING} ^(.+)=(.+)$
RewriteRule ^index.php$ http://photos %1/%2? [L,NC,R=301]
Your order is reversed. Rewrite it in front of you
RewriteRule /(.+)\/(.+) index.php?$1=$2
The question is old but might be still relevant for others, so I suggest a slightly different general approach:
RewriteEngine On
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z]+) [NC]
RewriteRule (.*) /%1? [R=302,L]
Notes:
QUERY_STRING in the condition checks for a param name "key" and catches it's value
The rewrite rule adds the param value as directory using %1. Note the ? removes the original query part from end result

redirect url already rewritten in htaccess

I have written htaccess
my actual url is
https://www.example.com/index.php?route=information/partnership_form
rewritten as
https://www.example.com/for-brands/partner-with-us
which works fine as I have written rule as
`RewriteRule ^for-brands/partner-with-us$ https://www.example.com/index.php?route=information/partnership_form [NC,L]`
but I want redirection if some user visits direclty https://www.example.com/index.php?route=information/partnership_form
to
https://www.example.com/for-brands/partner-with-us
below is my code redirect but I've tried many ways form other links of stackoverflow still I cant find any solution
rewriterule ^index\.php?route=information\/partnership_form(.*)$ /for-sports-brands/partner-with-us$1 [r=301,nc]
You can not manipulate querystring like that. You need to use a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=information/partnership_form [NC]
RewriteRule ^index\.php$ /for-brands/partner-with-us? [NC,L,R]
The ? at the end of the rewrite target is important as it discards the old querystrings.

Redirecting Dynamic Url to Static Url

I have read a lot of suggestion, article but i could not fixed my problem. Here is my Url :
us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-education
ja/resources?id=366:video-clip
I want to redirect this URL to static URL. I have tried with .htaccess Like this :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI}
(/|.php|.html|.htm|.feed|.pdf|.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
Redirect 301
/tw/en/investment-opportunities/ultimate-banking-how-to-create-your-own-bank
http://takeshiyashima.com/tw/investment-opportunities/ultimate-banking-how-to-create-your-own-bank
Above URL Fixed and working now but the top URL i have mentioned are not working. I need help.
Please, give a more clear example and post the right link, as it is not clear if there is any symbol before 'ja/resources' and if it's a part of the link at all.
As I understand you want to take only the part:
'my-thought-of-the-day-problem-of-traditional-education'
and redirect to: 'http://takeshiyashima.com/tw/investment-opportunities/my-thought-of-the-day-problem-of-traditional-education'
If it's true here is the possible rule:
us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-education
RewriteRule ^\/?us/my-blog\?catid=\d+id=\d+\:(.*) http://takeshiyashima.com/tw/investment-opportunities/$1 [R=301,L]
Not tested and can be made shorter. If you give more details to the example I will try to help.
Update according to your comment
For ja/resources?id=366:video-clip to takeshiyashima.com/ja/resources it is:
RewriteRule ^\/?ja/resources.* http://takeshiyashima.com/ja/resources [R=301,L]
For 'us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-educatio‌​n', everything is clear, but I see no logic in '/1-my-thought', we have to have a clear pattern. If you say that
you want to take always first 2 words from 'my-thought-of-the-day-problem-of-traditional-educatio‌​n'
then we can make '/1-my-thought', but if you will have different strings like 'my-stuff-education-is-cool', then we will not be able to apply a pattern using regular expressions. Please, provide more original links (3+) you would like to change, so I can see if there is any pattern.
If there is no pattern at all it is possible to use, for example, RewriteMap for Apache with an additional script on the server side, which will handle your links, but it's a more complex way, so let's stick to regular rewrites for now. Waiting for 3+ more examples.

Remove Page Number from URL with .htaccess

I need page numbers from URLs of the form:
http://mydomain.com/index.php?showtopic=XXXX&page=XXXX&#entryXXXX
so they become
http://mydomain.com/index.php?showtopic=XXXX&#entryXXXX
where XXXX are integers
I've previously tried:
RewriteEngine on
RewriteRule ^(.*)showtopic=([0-9]+)&page=([0-9]+)(.*) http://mydomain.com/index.php?showtopic=$1$3 [QSA,L,R=301]
but to no avail. So I shortened it to:
RewriteEngine on
RewriteRule ^(.*)&page=([0-9]+)(.*)$ $1&$3 [QSA,L,R=301]
but still nowt. Is there anything wrong with the regex at all?
You can't match against the query string in a rewrite rule, you need to match against the %{QUERY_STRING} var inside a rewrite condition:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^showtopic=([^&]+)&page=([^&]+)(&.*)?$
RewriteRule ^index\.php$ /index.php?showtopic=%1%3 [L,R=301]
The #entryXXXX part of the URL is a fragment, and the server actually never sees that. It's a client/browser-side only thing. Hopefully, the browser is smart enough to re-append the fragment after getting redirected.