htaccess redirection loop issue - html

I have a static website that has several html pages. In order to not hide .html extensions from url, we have following rules in htaccess for each url:
RewriteRule ^website-development$ website-development.html
This works fine in that if you open site.com/website-development, it actually opens the website-development.html file but in url it does not show the .html extension.
Now, we need to redirect(or hide) .html urls to their corresponding urls without .html e.g. if someone opens site.com/website-development.html in their browser, the url should be shown as site.com/website-development.
To do this, I added following rule:
RewriteRule ^(.*)\.html$ /$1 [L,R]
But doing this results in indefinite redirection and so the browser just keeps redirecting and never renders the actual page. Can you please suggest how I can redirect both site.com/website-development and site.com/website-development.html to site.com/website-development (which actually is an html file - website-development.html ). Thanks.

Yes indeed, this rule will cause a redirect loop:
RewriteRule ^(.*)\.html$ /$1 [L,R]
It is due to the fact your REQUEST_URI is adding and removing .html in 2 different rules and mod_rewrite runs in a loop.
You can use this rule instead based on a RewriteCond that uses THE_REQUEST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Related

Is there a way to get all relative URL paths to serve a single absolute page URL?

What I'm looking for is a way for all paths like example.com/blog, example.com/about, example.com/burnt/toast, etc. to all go straight to example.com and get the same index.html file, from which I can respond to the different URL paths with JavaScript.
Is this possible without adding a physical redirect placeholder file for each of the extensions? Without having to use node.
What you are looking for is called a "front controller." It is usually implemented in .htaccess using a rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
That rule says: "except for files that actually exist, handle all URL paths with index.html."

want to redirect an html page as sub page of other page

Like in wordpress you can make a page as sub page of other page how can we do it with HTML files with the help of .htaccess?
I have HTML pages on my website like this
abc.com/1.html and abc.com/2.html
I want to remove the .html extention like this
abc.com/1 and abc.com/2
And I want abc.com/2 to redirect to
abc.com/1/2 Please do remember the 1 in this url is not a folder.Both these urls abc.com/1 and abc.com/2 are in the same folder.
How can I do it.
To mask the file extensions, first enable MultiViews into your VirtualHost configuration file or .htaccess:
Options +MultiViews
Then, you could just provide hyperlinks without the .html extension. The HTTP server will search for file by adding extension itself. But you can also add a rewrite rule in your .htaccess:
RewriteCond %{THE_REQUEST} /([^.]+)\.html
RewriteRule ^ /%1 [R=301]
Here you get the %{THE_REQUEST} and parse it to rewrite URL without the .html at the end of the filename.
To redirect 1/2/ to 2/:
RewriteRule ^1/2/(.*)$ 2/$1
Please note that in this second rule, you do not use the [R] flag because you do not want to change URL in the browser.
Be sure to enable RewriteEngine before using rewrite rules:
RewriteEngine On
For more information about URL rewriting: https://httpd.apache.org/docs/2.4/rewrite/intro.html
In order to use regex better to use mod_rewrite which is more powerful than mod_alias.
Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^answer-now(/.*|)$ http://www.abc..net/2/? [L,NC,R=301]

send id via url but hidden

I have an html link:
Link
I formatted the link destination with a htaccess file:
RewriteRule ^destination$ index.php?content=destination [L,NC,QSA]
The question is: How can I give a ID via URL to the destination?
the full link will be index.php?content=destination?ID=x
X will be a dynamic number
but I would like to show this in the url - it should be invisible.
But I don't know how I have to modify the rewriteRule to realize it.
Although not a pretty solution and it obviously comes with flaws, you could always use cookies.
set a cookie in PHP:
setcookie("page_id","987987");
and look for it in htaccess
RewriteCond %{HTTP_COOKIE} ^page_id=([0-9]*)$ [NC]
RewriteRule ^destination$ index.php?content=destination&id=%1 [L,NC,QSA]

Link to index page of website

Is there a way to link to the index page of a website without specifying the name of the index page or the full website URL?
I have this:
Home
But when I click the link, my address bar shows:
mydomain.com/index.html
I would like it to show:
mydomain.com
Can I do that without putting the full URL (mydomain.com) in the href? If so, how?
For future viewers, I also found this question helpful.
You can just do this:
Home
Any href preceded by a slash is relative the root directory.
It should be noted that when viewing a webpage from a local hard drive in a browser, this will cause the link not to function.
I know this post is old and has an answer. But here is my contribution that also supports index that is not exactly in the root directory
to goto
mydomain.com/index.html
without index.html showing in address bar
Home
this will always point to mydomain.com no matter where the directory your file was saved to.
to point to the index in a directory use this:
Home
this will point to mydomain.com/subdir for mydomain.com/subdir/index.html while the accepted answer will always point to mydomain.com (and this is not always the desired action)
Create a ".htaccess" file and upload to your host public folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.yourwebsite.com.ph/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
</IfModule>

Redirect only HTML files?

Part One
I want to .htaccess redirect all HTML files to the home page. I looked at this guy's question (htaccess redirect all html files), and wrote this code:
RewriteCond %{HTTP_HOST} ^pandamonia.us$ [OR]
RewriteCond %{HTTP_HOST} ^www.pandamonia.us$
RewriteRule .*\.html$ "http\:\/\/pandamonia\.us\/" [L]
but the problem is that it also redirects the homepage to itself, causing the universe to end.
So my question, is how can I redirect every HTML page that is not the homepage to the homepage.
 
Part Two
Exclude certain subfolders and domains in redirects
Try changing .* to .+ in the regexp, that should mean 'at least one character' instead of zero or more characters, so the empty string should be avoided.
Wait. The initial '/' is included. Try it like:
RewriteRule /.+\.html$ "http\:\/\/pandamonia\.us\/" [L]