Trouble with simple htaccess RewriteRule to remove %23 from end of url - html

I started seeing 404 errors in my logs for urls like:
http://site.example.com/foo/bar.html%23anchor
Clearly the #anchor was being encoded (probably in someone else's email that I can't control) resulting in broken links. The anchor links are not that important, but I don't want my users seeing 404 pages. I thought I could fix this with a simple rewrite, but nothing I have tried has worked and none of the SO answers I looked at worked.
The rewrite codes I tried worked perfectly in RegExr and regex101, but when I try it in my .htaccess, the bad link still results in a 404 error. I have other RewriteRules working, but I can't seem to remove the unwanted %23anchor from the end of the request.
RewriteEngine on
RewriteBase /site
## Externally redirect non-canonical domain requests to canonical domain. ###
## This rule works ###
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://site.example.com/$1 [R=301,NC,L]
## This rule doesn't work ###
RewriteRule ^(\.html)(%23)(.*)$ $1 [R=302,NE,L]
I need to change this:
http://site.example.com/foo/bar.html%23anchor
into this:
http://site.example.com/foo/bar.html
What am I missing?

You are missing everything in front of ".html". Try this rewrite:
RewriteRule ^(.*\.html)(%23)(.*)$ $1 [R=302,NE,L]
https://regex101.com/r/fV3oU3/1

Thank you for all of your suggestions, but none of them solved the initial problem. Because the RewriteBase rule affects all relative rewrites, I could not see a way to write this rule as I originally intended. In the end the only thing that seemed to work was to rewrite to an absolute path. This is the rule I ended up with. It is not flexible and only works for this page, but at least it fixes the specific broken link I am currently trying to correct:
RewriteRule ^(.*bar\.html)\x23.*$ http://site.example.com/foo/bar.html [R=302,NE,L,NC]
The above rule rewrites this: http://site.example.com/foo/bar.html%23anchor
to this: http://site.example.com/foo/bar.html

Replace your last rule with this:
RewriteRule ^(.+?\.html)\x23 /$1 [R=302,NE,L,NC]
%23 is matches by \x23 in RewriteRule.

Related

How to make your website https only?

I've got a working SSL certificate, but the only way for a person to visit my https website is by typing "https://" in the search bar. I've got a .htacces file that contains the following code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
And I also tried this line of code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But neither of them seem to work.
I've also read that there is a meta tag that sends all users to your https no matter what. But still have yet to find what the meta tag looks like.
Any help is appreciated. Thank you for your time
The basic idea is correct - you will need rewrite rules on your webserver for to make the site HTTPS only
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Please take a look at the following documentation: https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
Regarding why your rules are not working - you need to know the context of .htaccess file, from documentation:
.htaccess files (or "distributed configuration files") provide a way
to make configuration changes on a per-directory basis. A file,
containing one or more configuration directives, is placed in a
particular document directory, and the directives apply to that
directory, and all subdirectories thereof.
it is however better not to use .htaccess at all if possible since it has negative performance implications and the same thing can be achieved with <Directory> blocks. All of that is in here: https://httpd.apache.org/docs/current/howto/htaccess.html
Regarding the meta tag - all you need to do is add something like this:
<meta http-equiv="refresh" content="0; url=https://yoursite.com/">
The meta tag is definitely not the best practice of HTTP-2-HTTPS redirection due to the fact that it is easy to circumvent and outside of webmasters control since it's a client side redirect.

Make url so that I can also accept extra parameter in .htaccess

I have goggling all around but not able to find the right solution.
I have a simple HTML site that contains only HTML files(.html extension) like pricing.html,contact-us.html etc.
I want to make url so that it accept integer as a parameter like this
Currently: http://www.amznhosting.com/pricing.html
I want to make url so that I can also accept extra parameter like this:
http://www.amznhosting.com/pricing.html/2
This is what I did so far in .htaccess
RewriteRule ^(.*)$ pricing.html?params=$1 [L,QSA]
Its working but css,js and images are not getting load.
Anyone having any idea?
You can use the following rule :
RewriteEngine on
RewriteRule ^(.+)\.html/(.+)$ /$1.html?params=$2 [NC,L]
This will rewrite /file.html/path to /file.html?params=path .

How to force loading extension ".html"after a url via .htaccess?

How could I achieve this?
force load extension via .htaccess ".html" after a url?
Pretty simple, but I couldn't find something a stack article on this or something similar to tweak accordingly.
If someone visits Domain.com/about I want them to land on Domain.com/about.html
Simply put I want to force all url's to load .html after the url via .htaccess.
This is to cover visitors, etc. with old links that don't have .html at the end of the url.
Does this page helps you:
https://www.garron.me/en/bits/add-html-extension-nginx-apache-htaccess.html
Or take a look here, you can find a bunch of propositions.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

301 Redirect without slash on destination-url

i did a website relaunch and now i need 301 redirects to keep my page rank :)
My old website looked like this:
www.domain.com/start/
or www.domain.com/start
New Website looks like this:
www.domain.com/home.html
My Rewrite-Rule looks like this:
RewriteEngine On
RewriteRule ^start/ /home.html [R=301,L]
RewriteRule ^start /home.html [R=301,L]
without the slash it redirects to home.html fine, but with start/ it redirects to home.html/ and goes 404..
I read multiple solutions but none worked for me, so your my last hope and im in kind of a hurry, thats why i ask here.
Thank you in advance, Philipp
Use a single rewrite rule. with an optional slash denoted by \/?. Terminate the rewrite rule by a $ so that /start/foo doesn't also match.
RewriteEngine On
RewriteRule ^start\/?$ /home.html [R=301,L]
try to add backslash on second line
RewriteRule ^start\/ /home.html [R=301,L]
good luck!

Strange session behaviour in Google Chrome for Twitter OAuth

for some strange reason in Google Chrome one of my scripts is having serious problems. I'm setting up a page that requires twitter/facebook connections. Basically what you are doing is:
visit page (Facebook+Twitter class are started, some session settings are being set etc)
Click the connect button for one of the networks
fill in your connection details
sign up at our website (clicking submit).
Now everything works perfectly in IE/Firefox/Safari, however Chrome is doing some really crazy stuff. I would like to ask you to visit this page:
Not Important Anymore
Most likely in Firefox/IE/Safari it will just display an empty var_dump() of the $_SESSION variable. At first this is what it does in Chrome aswel, BUT! If you refresh the page once in Firefox/IE/Safari the session is still empty, but in Chrome it is showing some keys already.
I have absolutely no clue where these keys come from.. this is the content of viewsessions.php:
session_start();
echo '<pre>';
var_dump($_SESSION);
if(isset($_GET['u'])) {
unset($_SESSION);
}
As far as I know there cannot happen anything else but the above and Firefox/IE/Safari are showing the right behaviour.
It wouldn't be a big problem if everything was working fine, but al the 'requestoken_XXXX' session keys belong to the Twitter OAuth.. and because the requesttoken is refreshed on everypage the Authentication redirect to my website can not find a matching token and thus not validate the authentication.
Can anyone see what is happening? Is some page being called in the back? Is this some Chrome related issue that is known? I really don't have any clues left what this could be..
Thanks in advance.
Problem solved..
For some reason this file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /
# Bestaande bestanden of mappen uitsluiten
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?)$ $1 [L]
RewriteRule ^start/$ login.php [L,QSA]
RewriteRule ^logout/$ logout.php [L,QSA]
RewriteRule ^timeline/$ timeline.php [L,QSA]
RewriteRule ^(.*)$ index.php [L]
</IfModule>
Did some request to index.php (I guess...), seems a bit odd because I have stated that if it's an existing file or directory it should just open the file: RewriteRule ^(.*?)$ $1 [L] and I also flagged it as the last rule to follow.
Removing this last RewriteRule solved the problem. Still not sure why Chrome overrules the [L] parameter.. if anyone could explain it, that would be awesome.