How to redirect in htaccess with a wildcard - .htaccess

This is really bugging me, for something that should be real simple.
Trying to 301 redirect a URL (via htaccess) - in the following manner:
https://www.example.com/forums/sub-forum/index.php
needs to be redirected to:
https://www.example.com/forums/sub-forum/
Basically want to get rid of the index.php (note: the sub-forum is a wildcard, as it can be anything, e.g. football-forum or tennis-forum etc...)
So far I've managed to work out that I need to do something along these lines:
RedirectMatch 301 ^/forums/.*/index.php /forums/<wildcard>
However, I'm not sure what to put in instead of <wildcard> part. I've tried putting .*, but that doesn't work
Any ideas on how to achieve this? Your help would be much appreciated.
Many thanks.

Related

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

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!

redirect in htaccess with partly remove of a weblink

I am using simple mod-rewrite rule:
Redirect 301 /feeds/ https://some-interesting-website.../feed/index.php
It doesn't work because if I have some link:
/feeds/some-difficulf-link/many-files-and-folders.php than result is:
https://some-interesting-website.../feed/index.phpsome-difficult-link/many-files-and-folders.php
How could I remove a rest of a link after index.php?
It was really hard job. I was need to pay a lot of time and efforts in it to find a solution.
Looks like mod rewrite logic is not very clear. However this code worked perfect for me: Redirect 301 /feeds https://some-interesting-website.../feed/index.php?

Redirect to target url and add query string

I'm trying to redirect just one page on a site to another one using htaccess. I'm new to this, but have been reading and reading everywhere to figure out how to do this, but without success.
I would like to redirect www.example.se to www.example.se/index.php?template=<templatename>
Is that possible and how would I approach that? So sorry if I'm asking a stupid question here, but really can't find a solution and it's driving me crazy.
Try:
Redirect / /index.php?template=<templatename>
Add a 301 right after Redirect if you want it to be permanent.

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

Why can't I make my URLs rewrite in my htaccess file?

I've been messing with this for the last hour... visiting about 20 different Google tuts and I can't seem make my site rewrite the url in any way. I've even just tried changing .php to .html and I'm not having any luck.
Here's one of my many fails:
RewriteEngine on
RewriteRule viewItem(.*)\.htm$ /viewItem.php?id=$1
For this above one, I'm just trying to change my viewItem.php page to viewItem.html
My ultimate goal is to change hxxp://mydomain.com/viewItem.php?id=900 (as one example of an id) to hxxp://mydomain.com/details/
I'm then going to append the title on the end. I can do the append in php but being a newb to .htaccess, I'm apparently missing something glaringly obvious.
Any ideas?
Thanks much.
Did you miss the 'l' of 'html'?
RewriteRule viewItem(.*)\.html?$ /viewItem.php?id=$1

Resources