URL rewritten but indexed as Query String - .htaccess

I tried googling this but all it gave me was tutorials on how to rewrite so I thought I'd give it a shot asking it here.
The problem is that we have a htaccess file that rewrites the urls to a query string:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+\/*[^\/]*\/*.*)$ index.php?app_route=$1 [QSA,L]
This works and it seems google is picking it up, however when you click on the link in google it shows the query string that it's rewritten to. We only link to the 'nice' urls so we don't know where google picked this up. Google for site:sorellehaarmode.nl to see for yourself.
Does anyone have experience with, or a solution to this problem?

Try adding these rules (anywhere to your current htaccess file, below the rules you already have is fine):
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?app_route=([^&\ ]+)&?([^\ ]*)
RewriteRule ^/?index\.php$ /%1?%2 [L,R=301]
This should permanently point the URLs with query strings to the ones without.

Related

Rewrite multiple rules in .htaccess / remove .html extension [duplicate]

How to remove .html from the URL of a static page?
Also, I need to redirect any url with .html to the one without it. (i.e. www.example.com/page.html to www.example.com/page ).
I think some explanation of Jon's answer would be constructive. The following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
checks that if the specified file or directory respectively doesn't exist, then the rewrite rule proceeds:
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
But what does that mean? It uses regex (regular expressions). Here is a little something I made earlier...
I think that's correct.
NOTE: When testing your .htaccess do not use 301 redirects. Use 302 until finished testing, as the browser will cache 301s. See https://stackoverflow.com/a/9204355/3217306
Update: I was slightly mistaken, . matches all characters except newlines, so includes whitespace. Also, here is a helpful regex cheat sheet
Sources:
http://community.sitepoint.com/t/what-does-this-mean-rewritecond-request-filename-f-d/2034/2
https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
To remove the .html extension from your urls, you can use the following code in root/htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
NOTE: If you want to remove any other extension, for example to remove the .php extension, just replace the html everywhere with php in the code above.
Also see this How to remove .html and .php from URLs using htaccess` .
This should work for you:
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
With .htaccess under apache you can do the redirect like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
As for removing of .html from the url, simply link to the page without .html
page
You will need to make sure you have Options -MultiViews as well.
None of the above worked for me on a standard cPanel host.
This worked:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
For those who are using Firebase hosting none of the answers will work on this page. Because you can't use .htaccess in Firebase hosting. You will have to configure the firebase.json file. Just add the line "cleanUrls": true in your file and save it. That's it.
After adding the line firebase.json will look like this :
{
"hosting": {
"public": "public",
"cleanUrls": true,
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
Thanks for your replies. I have already solved my problem. Suppose I have my pages under http://www.yoursite.com/html, the following .htaccess rules apply.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/
RewriteRule .* http://localhost/html/%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/
RewriteRule .* %1.html [L]
</IfModule>
Good question, but it seems to have confused people. The answers are almost equally divided between those who thought Dave (the OP) was saving his HTML pages without the .html extension, and those who thought he was saving them as normal (with .html), but wanting the URL to show up without. While the question could have been worded a little better, I think it’s clear what he meant. If he was saving pages without .html, his two question (‘how to remove .html') and (how to ‘redirect any url with .html’) would be exactly the same question! So that interpretation doesn’t make much sense. Also, his first comment (about avoiding an infinite loop) and his own answer seem to confirm this.
So let’s start by rephrasing the question and breaking down the task. We want to accomplish two things:
Visibly remove the .html if it’s part of the requested URL (e.g. /page.html)
Point the cropped URL (e.g. /page) back to the actual file (/page.html).
There’s nothing difficult about doing either of these things. (We could achieve the second one simply by enabling MultiViews.) The challenge here is doing them both without creating an infinite loop.
Dave’s own answer got the job done, but it’s pretty convoluted and not at all portable. (Sorry Dave.) Łukasz Habrzyk seems to have cleaned up Anmol’s answer, and finally Amit Verma improved on them both. However, none of them explained how their solutions solved the fundamental problem—how to avoid an infinite loop. As I understand it, they work because THE_REQUEST variable holds the original request from the browser. As such, the condition (RewriteCond %{THE_REQUEST}) only gets triggered once. Since it doesn’t get triggered upon a rewrite, you avoid the infinite loop scenario. But then you're dealing with the full HTTP request—GET, HTTP and all—which partly explains some of the uglier regex examples on this page.
I’m going to offer one more approach, which I think is easier to understand. I hope this helps future readers understand the code they’re using, rather than just copying and pasting code they barely understand and hoping for the best.
RewriteEngine on
# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [nocase]
RewriteRule ^ /%1 [L,R=301]
# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]
Let’s break it down…
The first rule is pretty simple. The condition matches any URL ending in .html (or .htm) and redirects to the URL without the filename extension. It's a permanent redirect to indicate that the cropped URL is the canonical one.
The second rule is simple too. The first condition will only pass if the requested filename is not a valid directory (!-d). The second will only pass if the filename refers to a valid file (-f) with the .html extension added. If both conditions pass, the rewrite rule simply adds ‘.html’ to the filename. And then the magic happens… [END]. Yep, that’s all it takes to prevent an infinite loop. The Apache RewriteRule Flags documentation explains it:
Using the [END] flag terminates not only the current round of rewrite
processing (like [L]) but also prevents any subsequent rewrite
processing from occurring in per-directory (htaccess) context.
Resorting to using .htaccess to rewrite the URLs for static HTML is generally not only unnecessary, but also bad for you website's performance. Enabling .htaccess is also an unnecessary security vulnerability - turning it off eliminates a significant number of potential issues. The same rules for each .htaccess file can instead go in a <Directory> section for that directory, and it will be more performant if you then set AllowOverride None because it won't need to check each directory for a .htaccess file, and more secure because an attacker can't change the vhost config without root access.
If you don't need .htaccess in a VPS environment, you can disable it entirely and get better performance from your web server.
All you need to do is move your individual files from a structure like this:
index.html
about.html
products.html
terms.html
To a structure like this:
index.html
about/index.html
products/index.html
terms/index.html
Your web server will then render the appropriate pages - if you load /about/, it will treat that as /about/index.html.
This won't rewrite the URL if anyone visits the old one, though, so it would need redirects to be in place if it was retroactively applied to an existing site.
I use this .htacess for removing .html extantion from my url site, please verify this is correct code:
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.proofers.co.uk/new} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.proofers.co.uk/new/$1 [R=301,L]
Making my own contribution to this question by improving the answer from #amit-verma (https://stackoverflow.com/a/34726322/2837434) :
In my case I had an issue where RewriteCond %{REQUEST_FILENAME}.html -f was triggering (believing the file existed) even when I was not expecting it :
%{REQUEST_FILENAME}.html was giving me /var/www/example.com/page.html for all these cases :
www.example.com/page (expected)
www.example.com/page/ (also quite expected)
www.example.com/page/subpage (not expected)
So the file it was trying to load (believing if was /var/www/example.com/page.html) were :
www.example.com/page => /var/www/example/page.html (ok)
www.example.com/page/ => /var/www/example/page/.html (not ok)
www.example.com/page/subpage => /var/www/example/page/subpage.html (not ok)
Only the first one is actually pointing to an existing file, other requests were giving me 500 errors as it kept believing the file existed and appending .html repeatedly.
The solution for me was to replace RewriteCond %{REQUEST_FILENAME}.html -f with RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
Here is my entire .htaccess (I also added a rule to redirect the user from /index to /) :
# Redirect "/page.html" to "/page" (only if "/pages.html" exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} /(.+)\.html [NC]
RewriteRule ^(.+)\.html$ /$1 [NC,R=301,L]
# redirect "/index" to "/"
RewriteRule ^index$ / [NC,R=301,L]
# Load "/page.html" when requesting "/page" (only if "/pages.html" exists)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^ /%{REQUEST_URI}.html [QSA,L]
Here is a result example to help you understand all the cases :
Considering I have only 2 html files on my server (index.html & page.html)
www.example.com/index.html => redirects to www.example.com
www.example.com/index => redirects to www.example.com
www.example.com => renders /var/www/example.com/index.html
www.example.com/page.html => redirects to www.example.com/page
www.example.com/page => renders /var/www/example.com/page.html
www.example.com/page/subpage => returns 404 not found
www.example.com/index.html/ => returns 404 not found
www.example.com/page.html/ => returns 404 not found
www.example.com/test.html => returns 404 not found
No more 500 errors 🚀
Also, just to help you debug your redirections, consider disabling the network cache in your browser (as old 301 redirections my be in cache, wich may cause some headaches 😅):
first create a .htaccess file and set contents to -
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
next remove .html from all your files eg. test.html became just test and also if you wanna open a file from another file then also remove .html from it and just file name
Use a hash tag.
May not be exactly what you want but it solves the problem of removing the extension.
Say you have a html page saved as about.html and you don't want that pesky extension you could use a hash tag and redirect to the correct page.
switch(window.location.hash.substring(1)){
case 'about':
window.location = 'about.html';
break;
}
Routing to yoursite.com#about will take you to yoursite.com/about.html. I used this to make my links cleaner.
To remove the .html extension from your URLs, you can use the following code in root/htaccess :
#mode_rerwrite start here
RewriteEngine On
# does not apply to existing directores, meaning that if the folder exists on server then don't change anything and don't run the rule.
RewriteCond %{REQUEST_FILENAME} !-d
#Check for file in directory with .html extension
RewriteCond %{REQUEST_FILENAME}\.html !-f
#Here we actually show the page that has .html extension
RewriteRule ^(.*)$ $1.html [NC,L]
Thanks
For this, you have to rewrite the URL from /page.html to /page
You can easily implement this on any extension like .html .php etc
RewriteRule ^(.*).html$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
You will get a URL something like this:
example.com/page.html to example.com/page
Please note both URLs below will be accessible
example.com/page.html and example.com/page
If you don't want to show page.html
Try this
RewriteRule ^(.*).html$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
More info here
If you have a small static website and HTML files are in the root directory.
Open every HTML file and make the next changes:
Replace href="index.html" with href="/".
Remove .html in all local links. For example: "href="about.html"" should look like "href="about"".
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*).html\ HTTP/
RewriteRule .* https://example.com/html/%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /html/(.*)\ HTTP/
RewriteRule .* %1.html [L]
it might work because its working in my case
RewriteRule /(.+)(\.html)$ /$1 [R=301,L]
Try this :) don't know if it works.

Using mod_rewrite / RewriteRule to create SEO friendly URLs

I'm trying to get this unfriendly link:
http://test.mysite.com/featured1new.php?homedetails=12-Magnolia-Court-Branson-MO-65616&ID=11315943&PHOTOID=20140401022411163178000000
To display as this friendly link when using my website:
http://test.mysite.com/homedetails/12-Magnolia-Court-Branson-MO-65616/ID/11315943/PHOTOID/20140401022411163178000000
This is the code in my .htaccess file in the main directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule homedetails/(.*)/ID/(.*)/PHOTOID/(.*)/ featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3
RewriteRule homedetails/(.*)/ID/(.*)/PHOTOID/(.*) featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3
The problem is the link still displays the old way in the website, however, if I key the new RewriteRule way directly into the browser, it works fine. So part of this is working the way I need it too.
Am I supposed to change my html code in the website to match/use the RewriteRule? (After reading on this site for quite some time, I didn't think I needed to do that) Thanks for any help
The problem is the link still displays the old way in the website
Your rule simply allows new url format and internally rewrites it to its old format equivalent.
If you want old format to redirect to new format (without an infinite loop), you have to add some code.
You can replace your current code by this one
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/featured1new\.php\?homedetails=([^&\s]+)&ID=([^&\s]+)&PHOTOID=([^&\s]+)\s [NC]
RewriteRule ^ /homedetails/%1/ID/%2/PHOTOID/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^homedetails/([^/]+)/ID/([^/]+)/PHOTOID/([^/]+)/?$ /featured1new.php?homedetails=$1&ID=$2&PHOTOID=$3 [L]
NB: even if this code redirects old format, it's better to replace your links with new format

specific redirect required using mod_rewrite

I have to do a very specific url redirect using mod_rewrite within an .htaccess. Below is a url which has to map to the url below it:
m.example.com/123456/123456-product-name/
This needs to map to the following:
m.example.com/product-name/123456
I'm still getting to grips with regex and url rewrites and I've spent a good couple of hours trying to get this right. Can anybody help!?
Thanks in advance
You can do that. in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^\d+/(\d+)-([^/]+) /$2/$1 [R=301,L]
Or just [L] (and not [R=301,L]), if you do that without redirection.

Rewrite url for indicization

I made a dynamic website but to be indexed on Google, I understand that I have to rewrite all URLs.
Every URL is in this form:
www.mysite.com/folder/?page=name
Where name is the name (with a lot of imagination) of pages.
I want that URLs in this form:
www.mysite.com/folder/name.php
To do so I need to use the .htaccess file but I can't understand what I should write in it. I read a lot of pages on internet but still I can't understand what should I do or what code I have to use.
Try adding this rule to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+folder/(?:index.php|)\?page=([^&\ ]+)
RewriteRule ^ /folder/%1.php? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder/([^/.]+)\.php$ /folder/?page=$1 [L]

Create .htaccess with a variable

I am having trouble finding a solution for what I want to do. I am going to have a couple thounsand affiliates signing up on our site. They will provide a username, and basically give out the url www.domain.com/affiliate/username to their clients.
So, the url I will be starting with is www.domain.com/affiliate/home.html?userid=bob (if bob is the username they give).
I need that to check with the mysql database if that userid exists, then redirect it to the www.domain.com/affiliate/bob url.
Right now, I have this in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)$ affiliate/home.html?affiliate=$1 [L]
It obviously doesnt work and Ive never dealt with .htaccess before. Any help is greatly appreciated. Thank you.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^affiliate/([^/]+)/?$ /affiliate/home.html?affiliate=$1 [L]
As for redirecting to the nicer looking URL if someone enters the one with the query string:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /affiliate/home.html\?affiliate=([^\ &]+)&?([^\ ]*)
RewriteRule ^ /affiliate/%2?%3 [L,R=301]
There's no way to check if a username is in your database before the rewrite happens, using an htaccess file. There's ways to talk to a database in apache 2.4 or using a RewriteMap (which can only be defined in server or vhost config, not in htaccess) but you're better off doing the check in your home.html and just return a 404 if the user isn't found.
See my answer on an other similar question.
You want www.domain.com/affiliate/home.html?userid=bob to be redirected to /affliate/bob. You want /affliate/bob to be rewritten to /affiliate/home.html?userid=bob.
Add this to your .htaccess in your /www/ or /public_html/.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^affiliate=([^&]*)$
RewriteRule ^affiliate/home\.html /affiliate/%1? [R,L]
RewriteRule ^affiliate/(.*)$ affiliate/home.html?affiliate=$1&r=0 [L]
The first rule matches if the user tries to access affiliate/home.html?userid=bob. The RewriteCondition matches the part of the query string (after the ?) that you need to write the fancy url. %1 matches the first 'capturing group' in a rewrite condition. The ? at the end will clear the query string. [R] means it is a redirect (see the linked answer what that does). [L] means it will stop matching if this one matches.
The second rule is an internal rewrite. $1 matches the first 'capturing group' in the RewriteRule. The trailing &r=0 is there to stop the first rule from matching. It requires a RewriteCond %{REQUEST_FILENAME} !-f to function properly, because the fancy url is in the same directory is the file that handles it.

Resources