htaccess ReWriteRule strip URL - apache

I am looking for a bit of help, i'm new to using .htaccess files and i'm trying to take my currently URL (below)
www.domain.co.uk?referrer=firstname&surname&age&email&DPECODE
and replace it with a more concise version.
www.domain.co.uk/
The issue is whilst trying to find an answer it refers to use a fixed word within the url which i won't have, as it will be a different URL each time.

Related

.htaccess redirect based on a part of URL

After site crash a redirect php script doesn't work as expected.
We try to fix it, but in the meantime we are looking for a quick solution to redirect search engine results so our visitors can at least visit after clicking a relative web page.
The url structure or the search engines result are something like this:
https://www.example.com/MainCategory/SubCategory_1/SubCategory_2/Product?page=1
and I'd like to redirect using the "SubCategory_2" part of the URL to something like this
https://www.example.com/SubCategory_2.php
so until we fully repair the script at least our visitors will se a relative web page.
I'm quite stuck... Any ideas?
Thank you
To redirect the stated URL, where all parts are variable (including an entirely variable, but present query string) then you can do something like the following using mod_rewrite near the top of your root .htaccess file (or crucially, before any existing internal rewrites):
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^[^/]+/[^/]+/([^/]+)/[^/]+$ /$1.php [QSD,R=302,L]
The QSD flag is necessary to discard the original query string from the redirected response.
The above will redirect:
/MainCategory/SubCategory_1/SubCategory_2/Product?page=1 to /SubCategory_2.php
/foo/bar/baz/qux?something to /baz.php
You can test it here using this htaccess tester.
UPDATE:
unfortunately without success. I get 404 error.
You'll get a 404 if the directive did not match the requested URL, or /SubCategory_2.php does not exist.
Is the URL redirected? What do you see in the browser's address bar?
If there was no redirect then the above rule did not match the requested URL and the rule did nothing. Either because:
The URL format is not as stated in the question.
The rule is in the wrong place in the .htaccess file. As stated, this rule needs to be near the top of the config file.
I found a basic solution here htaccess redirect if URL contains a certain string I crate something like this RewriteRule ^(.*)SubCategory_2(.*)$ https://example.com/SubCategory_2.php[L,R=301] and works just fine. My problem is that this is a "static solution" since "SubCategory_2" is a variable.
Ok, but that is a very generic (arguably "too generic") solution for the problem you appear to be attempting to solve. This matches "SubCategory_2" anywhere in the URL-path (not just whole path segments) and preserves any query string (present on your example URL) through the redirect. So, this would not perform the stated redirect on the example URL in your question.
However, the directive you've posted (which you say "works just fine") cannot possibly work as written, at least not by itself. Ignoring the missing space (a typo I assume) before the flags argument, this would result in an endless redirect loop, since the target URL /SubCategory_2.php also matches the regex ^(.*)SubCategory_2(.*)$.
Also, should this be a 301 (permanent) redirect? You seem to imply this is a "temporary" solution?
HOWEVER, it's not technically possible to make "SubCategory_2" entirely variable in this "basic solution" and search for this variable "something" in a larger string and redirect to "something.php". How do you know that you have found the correct part of a much larger URL? You need to be more specific about what you are searching for.
In your original question you are extracting the 3rd path segment in a URL-path that consists of 4 path segments and a query string. That is a perfectly reasonable pattern, but you can't extract "something" when you don't know what or where "something" is.

Removing file name and swapping forward slashes for dashes with mod_rewrite

Im in the middle of moving my blog from a dynamic site to a static site. As a part of not breaking too many links I'm trying to learn mod_rewrite but getting no where with the following RewriteRule:
My old url
/blog/index.cfm/2012/10/9/My-blog-post
My new url
/blog/2012-10-09-My-blog-post.html
So far I have the following in my vhosts.conf file but its not working
RewriteRule ([a-zA-Z0-9.]+)(/blog/index.cfm/)([0-9]+)(/)([0-9]+)(/)([0-9]+)(/)([a-zA-Z0-9-]+) $1/blog/$3-$5-$7.html
Any help/pointers would be great
First, you only need to bracket (i.e. capture) things you will be using on the RHS of your rule. But the problem appears to be that you are only copying the year, month and day to the RHS, but not the My-blog-post. Try this:
RewriteRule ([a-zA-Z0-9.]+)/blog/index.cfm/([0-9]+)/([0-9]+)/([0-9]+)/([a-zA-Z0-9-]+) $1/blog/$2-$3-$4-$5.html

We've created a new website, but the old URLs should continue to work because people have bookmarked them

The only problem is
The old urls are something like this www.example.com/?pt#!/2/1270/something-etc-etc/
and we want to redirect them, but we need to pass the something-etc-etc to the new url.
Something like this new.example.com/old/ plus(something-etc-etc)
I've been trying so many ways that I'm already lost
RewriteCond %{QUERY_STRING} ([:alnum:]-)+?[:alnum:]/$
RedirectMatch www.example.com/ http://new.example.com/old/
I was hoping that this regex will return only the ending part, but instead, it returns ?pt#!/2/1270/something-etc-etc/
Best way is to use the Apache mod_rewrite module as described in the URL Rewriting Guide. A bit of a heavy read if you haven't used mod_rewrite before, but well well worth learning. Lots of examples to make things concrete, too.

Apache URL Redirect Alternatives

One of my clients (before I came along) decided to use htaccess redirects as their form of URL shortening/search engine friendly URLs. They have literally thousands of them.
The new version of the site now has friendly urls but they aren't equivalent to their redirects so they still need them.
My question to you all is: Is there another way than to populate this file with thousands of lines of "Redirects /folder1 /folder2"?
Thanks
If you cannot make simple rules to catch all of them as in the #chris henry solution you can use the RewriteMap utility of mod_rewrite. You'll be able to write these thousand rules in a text file, then make this text file an hash file, and mode_rewrite will try to match url in this file (if it's an hash file it's quite fast). After that mode_rewwrite can generate a redirect 301 with the [L,R=301] tag.
Yep, look at using the Apache config (httpd.conf or httpd-vhosts.conf) to set up site wide folder aliasing. Eg:
Alias /folder1 c:/www/folder2
Look at http://httpd.apache.org/docs/2.0/mod/core.html#directory for more info.
Depending on how different the URLs being redirected are, one solution might be to come up with an rewrite rule that covers all of them, and maintain the short / long URLs in your application, or even a database.

Redirecting a Directory to a Script on Apache

So I'm playing with a script that makes it super easy to mirror images off of the web. The script works great (based off of the old imgred.com source, if you've seen that) problem is, it looks a little clunky when using it.
Currently, in order to use the script, you go to a url like:
http://mydomain.com/mirror/imgred.php?Image=http://otherdomain.com/image.jpg
What I'd like to do is to be able to go to:
http://mydomain.com/mirror/http://otherdomain.com/image.jpg
and have it redirect to the former URL, preferably transparent to the user.
I'm reasonably certain that this can be done via .htaccess with a MOD_REWRITE of some kind, but I'm getting frustrated trying to get that to work.
After messing with this myself, I found out that apache collapses any double slash in the URL before the query part into a single slash, and passes the result to mod_rewrite. Maybe that was giving you problems?
This might work for you (.htaccess in the mirror directory):
RewriteEngine On
RewriteBase /mirror
RewriteRule ^http(s?):/(.*) imgred.php?Image=http$1://$2 [L]
Don't know if your script accepts https addresses as well, so I included that just to be sure