.htaccess Rewrite directory/file to new directory/file - apache

I'm trying to rewrite a directory and filename at the same time using .htaccess, but for some reason I can't find anything about such a case on the net. Tried googling what not, but there is simply not an example for this.
When I access a php file in the following directory:
website.com/somedir/somefile.php
I want to open a file that is contained at:
website.com/dirone/dirtwo/dirthree/dirfour/anotherfile.php
Tried simply 301 redirecting the directory
website.com/somedir/
to
website.com/dirone/dirtwo/dirthree/dirfour/
with
Redirect 301 http://website.com/somedir/ http://website.com/dirone/dirtwo/dirthree/dirfour/
But that would only work if the file name was the same. However in my case the filename changes from somefile.php to anotherfile.php as well.
Also tried something like:
RewriteRule ^somedir/somefile\.php website.com/dirone/dirtwo/dirthree/dirfour/anotherfile.php [L,QSA,NC]
But it did not work.
Any help would gladly be appreciated.

Related

RewriteRule in htaccess returns the error Not found

I'm trying the second way to run PHP code in HTMl file using a rule in .htaccess file as described by the link https://stackoverflow.com/a/6237056/3208225
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php
But when I try opening my page e.g. test.html I receive
Not Found
The requested URL /test.html was not found on this server.
Why and how to resolve?
UPDATED (from comments)
I try both on localhost and on my shared hosting. htaccess and html files are in document root. BTW, the homepage (index.html) also returns Not found. In local machine the path is D:/Server/vhosts/another. And without this htaccess such virtual host works just fine. So there is no issue with its configuration.
With your shown samples please try following .htaccess rules file. Make sure your htaccess is present along with your php files only. Also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^(.*)\.html/?$ $1.php [NC,L]
Fix added by OP: Please note such rule is not execution of PHP code inside of HTML file, but just a redirecting from HTML file to PHP file, so the second file also must exist

htaccess redirection implied html extension not working

I've tried many things before coming here, it should be a simple problem, but there is something I miss for sure.
I want to redirect a bunch of URLs to another ones, one by one, and here is an example in my .htaccess file :
RewriteEngine On
Redirect 301 /index.php/Microcontrôleurs_Généralités https://newdomain.org/Microcontrôleurs_Généralités
The thing is that the old URLs are files in a real folder "index.php" but with ".html" extension.
When I go to https://olddomain.org/Microcontrôleurs_Généralités, apache serves me the implied .html file. I can go to https://olddomain.org/Microcontrôleurs_Généralités.html too, it's the same file on disk.
But my redirection as above does not redirect anything.
If I add the .html extension to the file like this :
RewriteEngine On
Redirect 301 /index.php/Microcontrôleurs_Généralités.html https://newdomain.org/Microcontrôleurs_Généralités
Then, if I go to the URL with explicit ".html" at end, it is redirected correctly, but if I miss the .html, apache says the URL was not found.
I've turned this in my head numerous times, I can't figure out the real problem.
Help would be much apreciated, thx.

How to use .htaccess to redirect missing files?

I've been hosting some files which aren't on the server anymore and apache error log keeps bugging me with the "file does not exist" line.
I'd like to use .htaccess to manually specify which files, if requested in the URI, should redirect to the root location (or any else, thereof).
Could anyone help me how to do that?
Example:
www.domain.com/non-existent-file.jpg should go to www.domain.com/
Solved it with:
RewriteRule non-existent-file.jpg http://www.domain.com/ [R=301]
Hope that's the best solution!

How do I rewrite www.sitename.com/thing/thing.php?otherthing=something-like-this to www.sitename.com/something-like-this?

How do I rewrite
www.sitename.com/thing/thing.php?otherthing=something-like-this
to
www.sitename.com/something-like-this?
please help me with this as I can't seem to succeed. My host uses apache 2.2. Many thanks for your help!
Update
No I don't need that trailing ? However, I used the Rewrite rule you offered me and it still ain't working. I also added a RewriteEngine On before the rules.
I have Linux hosting, .htaccess and the code is obviously semantically correct, cause otherwise I would get the all so popular 500 internal server error. I placed the .htaccess file in the folder thing and in the root of the site, but it still won't work.
There should be an option to display it in directory format instead of the PHP ? format. If not, you could use the .htaccess mod_rewrite rule to make that display in the /folder/ way.
The way I do it is that I just upload my files and each page name is index.html and then I create folders, and put each index.html in the folder. Like this:
/guidelines/
In that folder is index.html, so instead of it being /guidelines.html it's /guidelines/
Looks better without .html
You need to use mod_rewrite:
RewriteCond %{QUERY_STRING} ^otherthing=(.*)$
RewriteRule ^thing/thing.php$ /%1? [L]
No idea if you meant to have that trailing ? at the end of the rewrite, I don't think that's possible. Note that the ? at the end of the RewriteRule is to get rid of the query string, otherwise, the rewritten URL will still have the ?otherthing=something-like-this at the end.

Redirect entire site except one directory to a new site - apache .htaccess

I am trying to move my current site over to a new domain, except for ONE directory.
Example:
current site: oldsite.olddomain.example
new site: newdomain.example
So I know I can create an .htaccess redirect entry to do this, and it works, but I want ONE exception - I do NOT want to redirect a specific directory. I still want this to work:
http://oldsite.olddmain.example/myspecialdirectory/… and every file and directory under it.
Can someone help?
Thanks!
Try this mod_rewrite rule in the .htaccess file in the document root of oldsite.olddomain.example:
RewriteEngine on
RewriteRule !^myspecialdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]