.htaccess redirect to another page while retaining the complete query string - .htaccess

I am a little lost. I tried searching this site and the web at large, but I couldn't seem to find exactly what I need. So please forgive me if this has been answered before, I really tried to find it.
I have … inherited a .htaccess file with quite a lot of 301 redirects in the
Redirect 301 /shorthand /actual-link/actual-file.php
syntax (with a RewriteEngine On line somewhere high up in the file). I don't know exactly much about redirects, but that seems pretty straightforward to me. It just sits there and sometimes new shorthand links get added. There is some non-www to www, and http to https kind of stuff at the top, that's it.
Now the structure of the site changes, and two similar pages that process query parameters get consolidated into one. Basically there is
Old page: /path/subpath/index.php?some=query&parameter=or&other
New page: /other-path/file.php?added=parameter&some=query&parameter=or&other
I can't predict what parameters exactly will be part of the URL, I just have to take everything starting from the ? and append it to the new URL, that already has to include an added parameter, so everything after the ? follows ?added=parameter& .
I suppose that is not exactly hard, but alas, I lack the experience. And all I could find was something like "Take this specific defined query parameter you already know and set it as a path name" or vice versa, and I couldn't get that to work for my problem.
Is there a solution compatible with the syntax used elsewhere in the file? Or does that matter at all? Can I combine Redirect 301 … lines with RewriteCond … RewriteRule … commands? Does %{QUERY_STRING} help me somehow? If so, how can I figure out the correct syntax?
I would really appreciate if someone could point me in the right direction.
Many thanks in advance!

Related

Redirect issues: New category/article structure / How to get rid of the article id?

I guess, this is an easy one but anyway, I haven't figured it out yet.
After migrating my website from Joomla 3 to Joomla 4 the structure of categories and articles will change. That's why I will need some rules in .htaccess to redirect the old urls to the new ones.
The website is hosted on an Apache server.
The old URL structure looks something like that.
https://www.mydomain.de/category/subcategory/item/[articleID]-[articleAlias].html
[articleID] is a digit.
[articleAlias], e.g. „this-is-article-number-233“
This should be redirected to...
https://www.mydomain.de/newcategory/newsubcategory/[articleAlias].html
An example:
https://www.mydomain.de/category/subcategory/item/2324-this-is-my-latest-article.html
… should be redirected to...
https://www.mydomain.de/newcategory/newsubcategory/this-is-my-latest-article.html
I've played around with RedirectMatch and Rewrite Rule but haven't been successful to make it work. How do I get rid of the article id?
My latest try failed with...
RedirectMatch ^category/subcategory/item/([0-9]+)-(.*)$ /newcategory/newsubcategory/$1
Is there a simple and elegant solution to this? Thanks in advance!
UPDATE
Maybe it's more complex than I thought it was.
Main problem is that not only my categories changed but also the ids of the articles.
So, to stick with my example...
https://www.mydomain.de/category/subcategory/item/2324-this-is-my-latest-article.html
first turns into something like:
https://www.mydomain.de/newcategory/newsubcategory/1223-this-is-my-latest-article.html
Anyway, Joomla 4 is able to drop the article id automatically (guess with an internal rewrite) for seo-friendly URLs. I activated that feature to make the new URLs look like
https://www.mydomain.de/newcategory/newsubcategory/[articleAlias].html
The [articleAlias] stays the same.
A redirection according to what you actually ask should be possible like that:
RewriteEngine on
RedirectRule ^/?category/subcategory/item/[0-9]+-(.*)\.html$ /newcategory/newsubcategory/$1.html [R,L]
However I doubt that this really is what you want: this completely drops the numeric ID of the resource. Which means that it won't be available for processing when the redirected request comes back requesting the new, stripped URL. How do you want to internally rewrite that request back to the internal resource then, without that ID?
I made some more tests and it this is the final solution to my problem:
RedirectMatch 301 ^/?category/subcategory/item/[0-9]+-(.*)\.html$ /newcategory/newsubcategory/$1.html

Remove directories from URL using .htaccess

I've been searching around and tried many different ideas to fix the state of a URL for my client, but have had no joy.
The URL currently looks like this:
website.com/folder1/folder2/folder3/page.php
My client wants this to be showing much like the other links on the website as so:
website.com/page
I'm fine with removing the .php that's simple enough and is already written into my .htaccess file. It's just removing the 3 directories from the URL is what I cannot work out.
Before anyone asks about moving the file to a higher directory, I'm afraid this cannot be done as everything needs to be down in this 3rd directory for this page.
I believe my latest attempt is about the closest I've got to solving this, if anyone could point out my mistakes and a solution it would be much appreciated:
RewriteRule ^/folder1/folder2/folder3/(.*)$ /page$1 [L,R=301]
EDIT:
After researching more today, I think I may have confused people with this question. I think what I should have said is that I need to mask the URL, hiding the 3 directories, and showing just the domain and the page itself.
I still seem to be hitting a wall with masking too. Any advice?
After a whole weekend (and quite a bit of the week between working) I finally managed to succeed with what I wanted.
The site now displays as required:
website.com/page
Which has been redirected and masked from:
website.com/folder1/folder2/folder3/page.php
The piece of code required in the .htaccess is as follows:
RewriteRule ^page$ /folder1/folder2/folder3/page.php
Looks like I was being stupid before and had the syntax backwards, but all is well now and I'm allowed to sleep :)
Hope this helps anyone else in future with this sort of problem!

.htaccess redirect to subfolder, and remove it's name

I'm kind of noob in the world of web so my apologies... I tried many things found on SO and elsewhere, but I didn't manage to do what I want. And the Apache documentation is... well too much complete.
Basically what I want to do is redirect my domain to a subfolder. I found easy solutions for this (many different actually).
http://www.foo.com/
http://foo.com/
should redirect to /bar and appear as http://foo.com/
Using the following I got the expected result :
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.foo.com$
RewriteRule ^/?$ "http\:\/\/foo.com" [R=301,L]
RewriteRule ^((?!bar/).*)$ bar/$1 [NC,L]
But I also want the subfolder as well as filenames not to appear when explicitly entered, i.e :
http://www.foo.com/index.html
http://foo.com/index.html
http://wwww.foo.com/bar
http://foo.com/bar
http://wwww.foo.com/bar/index.html
http://foo.com/bar/index.html
Should all appear as
http://foo.com/
Is this possible ?
Obviously using .htaccess, since I'm on a virtual host.
Thanks
As Felipe says, it's not really possible, because you lose information when you do that R=301 redirect: a hard redirect like this starts a whole new request, with no memory of the previous request.
Of course, there are ways to do similar things. The easiest is to put the original request in the query string (here's a good rundown on how mod_rewrite works with query strings). Sure, the query string does show up in the URL, but most modern browsers hide the query string in the address bar, so if your goal is aesthethics, then this method would be workable.
If you really don't want to show any of the original query in the URL, you might use cookies by employing the CO flag (here are some very good examples about cookie manipulation). At any rate, the information about the original request must somehow be passed in the hard redirect.
But anyhow, and most importantly, why would you want to do something like this? It's bound to confuse humans and robots alike. Great many pages behaved like this back when frames were fashionable, and it was pretty terrible (no bookmarking, no easy linking to content, Google results with the snippet "your browser cannot handle frames", no reloading, erratic back button, oh boy, those were the days).
Speaking of which, if your content is html, you may just use a plain old iframe to achieve the effect (but I'd sincerely advise against it).

htaccess redirects that ignore query string

I've recently inherited a webserver whose IP previously belonged to a well known band's forums.
Problem is I'm now drowning in 404 errors!
The URLs in question look like this:
http://[server_ip]/forum/ucp.php?mode=register&coppa=0
http://[server_ip]/forum/viewtopic.php?f=3&t=45330
http://[server_ip]/forum/index.php+++++++++++++++++++++++++++++++++++++++++++++++Result:+%ED%E5+%...
http://[server_ip]/forum/viewtopic.php?f=3&t=44597&start=0
In an ideal world I would like to redirect any traffic going to /forum/ucp.php, /forum/viewtopic.php or /forum/index.php elsewhere regardless of query string.
Or anything going to /forum/.* elsewhere, if that's doable.
I've tried a number of different solutions with little success, any help appreciated.
Assuming that you want to redirect all traffic to /forum/.* to http://mysite.com/somedirectory, which you can replace with the actual URL you wish to redirect to, you can add the following to the .htaccess in the root directory of your sites domain.
RewriteEngine On
RewriteBase /
#for any request starting with forum
RewriteRule ^forum/ http://mysite.com/somedirectory? [NC,L,R=302]
Be sure to keep the ? if you want to remove the query string params from the original query. If you want to make it a permanent redirect, change the 302 to a 301.
Don't really know what you have tried so far but this site will probably help you.
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#redirects

htaccess redirect conditions

I figured out how to redirect someone, if they happen across one particular filename:
Redirect /index.php http://www.website.com/#myaccount
As you can see, I'm pretty much redirecting that visitor to the same page, which doesn't work. It's an endless look, regardless of the slight/minuscule change.
I want to force someone to see a part of the page, by adding the hash. (it's a little weird, I know)
I'm guessing this may be a time to use regex in the htaccess file.
But I thought I'd ask if there's a simpler way to do this from the htaccess file.
The fragment of an URI (the part after the first #) is not sent to the server. That means you cannot use server side techniques to test if the fragment has a certain value of does exist at all; only client side techniques like JavaScript can do that.

Resources