Pattern Matching htaccess redirect - .htaccess

I know this should be relatively simple but am having trouble and have read and tried several examples from here but none seem to work.
I need to temporarily stop one particular url displaying results and want
to instead redirect the user to another page.
The URL to match on looks like this:
http://www.testdomain.com/news/index.php?toptitle=big%2Bend&XMLFILE=http://anotherdomain/item.rss%3Fkeyword%3Dbig%2Bend
(The URL is longer than the above with more parameters)
But all I want is if the url has: toptitle=big%2Bend in it then I want to redirect the visitor to another subdirectory
Here's what I have tried:
Redirect 301 /(toptitle=big%2Bend)$ /block/
Then I tried:
RedirectMatch 301 ^/(toptitle=big%2Bend)/?$ /block/
I have tried it with and without encoding but doesn't seem to be working so must be doing something wrong.
Other things are working in the htaccess file so am a bit stumped now - any help appreciated.. I know it's basic :-(
Thanks in adavance

How about:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} toptitle=big%2Bend
RewriteRule ^(.*)$ block/? [R=301]
This requires the rewrite module to be enabled in the main config:
LoadModule rewrite_module modules/mod_rewrite.so

Related

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

Rewrite example.com/index.php/... to example.com/... using mod_rewrite and .htaccess

I am migrating my site from Wordpress to Jekyll and I would like to keep the URLs working. My idea was to use a .htaccess file for this and to place it in the root of the site. But unfortunately after trying several tutorials and generates it doesn't seem to work.
The old URLs have the following format
http://example.com/index.php/2016/05/07/title-of-the-blog-post/
The new URLs have this format:
http://example.com/2016/05/07/title-of-the-blog-post.html
Among others I've tried this example which looks good to me, but it actually casues all pages on my site to display an error message :)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^index.php.*$ http://example.com/ [R=301,L]
I think that should take all URLs that start with example.com/index.php and make them start with example.com/, but apparently that is not the case.
To redirect
http://example.com/index.php/2016/05/07/title-of-the-blog-post/
to
http://example.com/2016/05/07/title-of-the-blog-post.html
you can use the following rule :
RewriteEngine on
RewriteRule ^index\.php/(.+)$ http://example.com/$1.html [NE,L,R]
or alternatively you can use mod_alias like that:
RedirectMatch 301 ^/index\.php/(.+)$ http://example.com/$1.html

How to create a vanity url

I would like to know how to create a vanity url for a website.
Ideally I would like to be able to put this on a flyer:
www.charity.org.uk/monthlydonation
and when that is entered, it will go off to:
www.charity.org.uk/donate/monthly-donation.php
I've been reading about vanity urls, redirects and rewrites but quite frankly I'm not even sure what I need to do this?
I tried the following in a .htaccess file:
RewriteEngine On
RewriteBase /
RedirectMatch 301 /monthlydonation /donate/monthly-donation.php
but got an error message saying there was a redirect loop.
All time and help is greatly appreciated.
Try using mod_rewrite instead, RedirectMatch is part of mod_alias and processes the request separate from mod_rewrite:
RewriteEngine On
RewriteBase /
RewriteRule ^monthlydonation$ /donate/monthly-donation.php [L]
Additionally, the reason why you're getting a redirect loop is that RedirectMatch expects a regex and not just a path. So /monthlydonation is the matching pattern, and that happens to also match the redirect's target: "/donate /monthly-donation.php".

301 Redirect .mp4 file to a link

I have a link to a video file (example.com/abc.mp4), which, when clicked, I would like to redirect to another link. We don't want to change the link on the page since this link was given out, so it would make more sense to redirect it, if possible.
I would think there should be a way to do this via .htaccess and RegEx, but have not been able to have any luck finding a solution, or talk about something like this.
Does anyone have any ideas?
There are two chances: either you're doing the redirection rule wrong, or your Apache is ignoring the .htaccess directives. ¿Do you know if another rewrite rules are working? ¿Can you check if your vhost if configured with AllowOverride All?
Additionally, you could try the following rule to redirect a file which doesn't exist. Just to be sure:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^xyz\.mp4 http://www.newsite.com/newfile.mp4 [r=301,L]
</IfModule>

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

Resources