htaccess redirection implied html extension not working - apache

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.

Related

Redirect strange URLs with two periods in them

I had a static HTML website that I recently converted over to drupal. I have been monitoring my sites 404 errors in webmaster tools and drupal reports and have noticed that google has indexed strange urls. My guess is they came from relative links that were created improperly from the older static HTML site.
Here is an example:
www.example.com/items../item-page.html
The actual page is:
www.example.com/items/item-page.html
The new drupal site doesn't even have .html extensions. I am using URL redirect and path auto modules and have redirects setup for all of the old urls to make sure they are 301'd to the new URL structure (e.g. www.example.com/items/item-page.html would be 301 to www.example.com/items/item-page).
I have access to the server so I am doing my redirects in the apache httpd.conf file instead of .htaccess. I tried the following code to redirect ../ to / but I am not having any luck:
RewriteRule ^\.\./(.*) /$1 [R=301,NC,L]
This rule doesn't do anything when I go to a url with the ../ in it. Is there a rewriterule that can match ../ and remove it from any url?
NOTE: I have other redirects in apache httpd.conf that are working fine...such as:
RewriteRule ^items/pdf/(.*)$ /sites/default/files/documents/items/$1 [R=301,NC,L]
So I don't think its my server configuration.
EDIT:
I noticed that the rewrite rule above for rewriting the pdf directory works even with the .. in the URL. Example:
http://www.example.com/items../pdf/somedocument.pdf
redirects to
http://www.example.com/sites/default/files/documents/items/somedocument.pdf
So it looks like the .. is completely ignored in rewrite rules, which is why I can't get anything to work. Does anyone know a way around that?
I believe you need to escape your forward slash.
I believe rewriterule acts against the HTTP request URI, not the page link.
Consequently, I believe you will need to remove the carat to find a match, and analyze your intentional URLs to see if that would have a negative impact elsewhere.
You can use this
RewriteRule ^/items\.\.(.+)$ /items/$1 [L,R]
This will redirect /items..foobar to /items/foobar
I was not able to fix the issue using rewrite rules in apache due to the rewrite rules not finding ".." in the url for unknown reasons.
My solution was to create a custom drupal module that looks to see if ".." is in the URL. If the ".." string is found, then I have it set to redirect to the url without ".." in it using built-in drupal functions. Here is the code I used in my module.
function doubledot_fix_init() {
$destination = drupal_get_destination();
$alias = drupal_get_path_alias($destination['destination']);
$fixpath = str_replace("..", "", $alias, $count);
if ($count > 0) {
drupal_goto($fixpath, array(), 301);
}
}
I do not see any reason this fix will break anything, because ".." should never really be found in any URL. If anyone can think of a situation that this fix could cause an issue, or if you know of a better solution, please let me know.

htaccess 301 redirect "works" but I get a 404 error at new site

Sorry for such a long title, but it pretty well describes what is happening.
Details: I have two sites, different domains. Previously, I had a temporary site in a not-visible, but published directory in the older domain. Only those who had the extra directory (or the extra path would normally see the temporary site).
Now that I have a new domain and a permanent new site, I simply want to redirect any attempts to access the old directory/pages/site. Here is the line I added to the old site's htaccess file (last line, BTW):
redirect 301 /mailscamalert.com/weather2/ http://www.mid-southweather.com/
That "works" at least in the sense that the user ends up at the new site. But that site throws up a 404 'flag' and the user ends up at my "erer" page. All the new site's navigation is on that page, of course, but it is probably very confusing!
I've tried removing the trailing "/" on 'weather2/' and/or "...com/", adding
"index.html" to the new site's url. No change in ending up at the error page. Also have tried "meta" redirects and even a bit of php:
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mid-southweather.com/index.html");
Any helpful suggestions or links, greatly appreciated!
Thanks!
Please correct your rewrite rule to the following:
RewriteEngine On
RewriteRule ^weather2/ http://www.mid-southweather.com/ [R=301,L]
If you had multiple pages in that directory and want them all to redirect to your new target domain, do this:
RewriteRule ^weather2/.* http://www.mid-southweather.com/ [R=301,L]
Your current rewrite rule appears to append the actual directory you are trying to redirect to the target url.
Location: http://mid-southweather.com/weather2/
As I discovered using live http headers extension. That weather2 directory of course doesn't exist on your new site, thus the 404.
Just dump your lines and replace them with mine and it should all work nicely. And undo any other changes you may have done in the process of trying to get it working.
Make sure you don't have other rewrite rules going on, it looks to me like you might have one more running somewhere.

Strange behavior for missing files with short URLs

My webserver shows an error for missing pages. These URLs all 404:
http://my.web.site/missing.html
http://my.web.site/missing/
http://my.web.site/missing
But these URLs redirect to / with a 301 Moved Permanentlystatus code:
http://my.web.site/m/
http://my.web.site/m
Why do these URLs not 404? And how can I turn the 301 redirection off?
Since I don't have access to http.conf on this shared server (Apache), I am looking for a solution in .htaccess.
Just to clarify: What I want is that all missing pages 404.
Note:
The observed behavior (301 for one letter URLs) is the default on my (shared) webspace. There is no .htaccess file at this moment. I'm looking to resolve this issue with a .htaccess file.
The behavior described in my question is caused by the Apache module mod_spelling. Creating a .htaccess with
CheckSpelling Off
solved the problem.
I am disappointed that my bounty was awarded to an "answer" that (a) did not answer my question, (b) suggested a "solution" that would cause the exact behavior I was trying to avoid (301), and (c) my comment that points this out was ignored by the answerer.
as you know you don't have access of http.conf file so first you need to check that your htaccess rewrite is working or not
If it is working (for checking it you can write some environment variable
SetEnv APPLICATION_ENV dev_dev
and check in $_SERVER
Now time to do something in htaccess for 301 redirection
I have write this code in my HTACCESS
RewriteEngine on
RewriteRule ^testurl/?$ /home/? [L,NC,R=301]
I don't know you want to redirect from which URL to which , But
in my code xyz.com/testurl will redirect to xyz.com/home
here my application is codeIgniter so might be you have page extension .php or .html like this

How to redirect a full directory to another webpage page?

So basically, I would like to know how to redirect all files within a directory to a page on my website. I would like to do this via my .htaccess file.
I have a directory, directory1, and it contacts many files.
I need to redirect all files within directory1 to www.mydomain.com/webpage.html
Is this possible?
You need to add this before any other rules in your htaccess file:
RewriteRule ^directory1/? http://www.mydomain/webpage.html [L,R=301]
Try using the 301 redirect first, it's the simplest and probably fastest method. Otherwise your solution will require mod_rewrite engine to be on.
Redirect 301 /directory1/(.*) http://www.mydomain/webpage.html
If that doesn't work try RedirectMatch, I haven't used either of these in a while myself so I'm just shooting from the hip by memory.
RedirectMatch 301 /directory1/(.*) http://www.mydomain/webpage.html

How can I redirect people accessing my files as directories?

I have the following situation:
On my webserver I have an instance of websvn running, where specific repositories and revisions can be accessed by a URL like
http://www.myhost.com/listing.php?repname=repository1&path=%2Ftrunk%2Fbackend
Somehow, out there in the wild, a wrong URL is being used to access this
http://www.myhost.com/listing.php/?repname=repository1&path=%2Ftrunk%2Fbackend
(Notice the slash after listing.php)
Now, although the URL works and websvn still shows the webpage, images and stylesheets do not get loaded correctly, since they are referenced relative.
I tried to add an .htaccess file to the webroot to redirect people accessing the file as directory to the correct URL.
I have tried multiple variations and ended up with this file:
RewriteEngine on
RewriteRule ^/listing.php/ listing.php [R=301,QSA]
But, since I am writing here, you already guessed it: It doesn't work.
I also tried
RewriteEngine on
RewriteRule ^/listing.php(.*) listing.php$1 [R=301,QSA]
What am I doing wrong?
Perhaps among other things, a RewriteRule within .htaccess that starts with “^/” will never match anything at all. (Examples that include a leading slash are for the global configuration file.) Remove the leading forward slash and see if that helps.
Also, I recommend changing the 301 to a 307 until you get it working. Otherwise, your browser will cache the 301 result, redirecting on subsequent references without consulting your server at all and likely giving you very confusing results.