I'm not amazing with web stuff but I have a small portfolio site which I am redesigning. I am looking to hide the example.com/page.html and make it website.com/page.
I added the rewrite engine which I found on here for the .htaccess
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ $1.html
The only problem is that when I use the RewriteEngine, the original path of /page.html loads as normal, but when i test it as /page/ it seems to lose the style sheet, I've tried relinking the style sheet as .../style.css instead of just style.css, but still all the images and style sheet seem to go missing.
It's probably something pretty damn simple but I need some help here.
what i understood from your words is that you want to make your url like :
http://www.example.com/page
ok . if you don't have to use htaccess , you can do this to do what you want .
just change the file name (page.html) to (index.html) . now create a new folder in your root and move the index.html to there !
and if the file , (page.html) is your home page , just change its name into index.html and leave it alone :D !
Your problem doesn't necessarily have to do with the RewriteRules, but with paths. You should, in general, always use absolute paths to images and assets. So instead of loading style.css or ../style.css, load /style.css or /static/styles/style.css. Otherwise the browser tries to resolve relative to the page location, and /page is considered to be in the root folder while /page/ is thought to be its own folder.
Iow, if you load style.css from your page:
From /page it will load /style.css
From /page/ it will load /page/style.css
Always use absolute paths, save yourself the pain and frustration.
Apart from that, ensure the images and assets are also not rewritten into .html extensions as mentioned in the other answers.
You probable don't want to rewrite every possible extension, so you might want to try something like this:
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
What this should do is check to see if a file exists with an .html extension. If it does it should transparently append .html to the path prior to doing the actual "lookup". So, if you make an HTTP request for
http://yourdomain.tld/somepage
and your site has an HTML page called somepage.html, the actual URL that gets processed will actually be
http://yourdomain.tld/somepage.html
EDIT:
I'm including a Dropbox link for a self-contained example that shows the suggestion above works: https://www.dropbox.com/s/6md9gviv0r2rf9v/xampp.7z
It contains a portable version of Xampp + the source files from this rather nice tutorial: http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/
Unpack the xampp.7z file somewhere (I recommend the Desktop) and then find and execute the setup_xampp.bat file. It will adjust all the internal paths to your local filesystem. Then, run xampp-control and start Apache. Once that's running, navigate to http://localhost:8080/testsite/ - this is the test site. You should be able to bounce back and forth from http://localhost:8080/testsite/ to http://localhost:8080/testsite/contact - both pages have a .html extension.
HTH.
Related
I am designing a portfolio site for a client. I am using Semplice template for Wordpress as it is supposed to be a good theme, but the thing is that is not so customizable as I would like, so I've designed the home page with HTML, CSS and JS on my own, with no templates. So I want to use the index.html I've created only for the home page, and Wordpress+Semplices for the rest of the sections of the website.
Now, I've already uploaded the Wordpress content and database to the host, and it's working. I am uploading my HTML+CSS+JS through the CPanel, but I here comes the problem:
I can upload my files to the root directory of the Wordpress in the public_html folder. If I do it, It keeps using the index.php (which charges the wordpress index, that I don't want), unless I delete this file and leave only the index.html that I've designed, in which case it runs my index but it doesn't loads the rest of the Wordpress even if I write the links to the sections properly. So it looks like this default index.php charges the rest of my wordpress them, and if I delete it, it won'g load anymore.
So, to conclude, I need something that charges both of them, the index.php which will load the rest of the wordpress sections (work, about me, info, etc.) but that uses my index.html as the default home that appears when I write the URL in the browser.
I will show you the files of my public_html folder, just in case it can clarify something for you to help me and understand me better:
By the way, as you can see in the image, I have no .htaccess file in my folder. That's confusing. What's the reason?
The .htaccess file can be not visible, try unhiding it in your File Manager -> Settings -> Show hidden files. If it's still not there, you can create .htaccess file yourself.
After that, it should be an easy redirection with the .htaccess file as this:
RewriteRule ^$ http://example.com/path-to-html.html [L,R=301]
I’m planning in doing a web site for a personal project and I would like to personalize the URL but I couldn’t find how to do that.
Instead of the url being www.mywebsite.com/example I would like to change to www.example.mywebsite.com.
Another thing that I would like to do is remove the file extension, i.e.: www.mywebsite.com/example instead of www.mywebsite.com/example.html
I think that these stuff need to be done in .htaccess but I don’t have idea how to do it.
the first point you can do with subdomains, that you see in your hosting account or question with technical support. The second point of the extension of the html you can do in the .htaccess as you say:
If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to alter the last line from the code above to match the filename:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Source: https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
I am currently trying to get to grips with .htaccess.
On my website, my about.html page (which is just a basic text HTML page) works fine with what I have managed so far:
mywebsite.com/about.html
works and shows as:
mywebsite.com/about
I have another section called:
mywebsite.com/works/
Which is full of .jpg and .html files and several folders. I currently have:
mywebsite.com/works.html
working which opens up that page and displays all my works as thumbnail .html links which can be clicked and accessed.
I want the website to display this page as:
mywebsite.com/works
instead of:
mywebsite.com/works.html
I don't have any issues with my about.html page because it is just one HTML page, but as works is a folder, when I type:
mywebsite.com/works.html
it shows up as it should but when I remove the .html I get:
mywebsite.com/works/
As a Fancy Directory listing!
How can I simply change this so that it just shows up as:
mywebsite.com/works
Create like this
works/index.html
And try to access like
www.example.com/works/
you can put a copy of works.html with name of index.html this can solve your purpose.
you can also restrict listing by adding
'Options -Indexes' in your .htaccess.
Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.
This should also work in a sub-directory path:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
What I have
I have created a folder called "r" in my server and I have put all css, js and images in it, with respective subfolders.
E.g.:
/
|public_html
|r
|styles
|js
|img
What I want
Web pages and stylesheets, who send a call for rescuing files, must have a pattern as: "resources/(.+)".
E.g.
In html document <script src="resources/js/jquery.js"></script>
In css document #import url('resources/css/fontawesome/css/font-awesome.min.css');
What I did
I tried to set the .htaccess file in public_html (because the server requires to publish the web site in it) with Alias and AliasMatch but the server catched an internal error.
Then I tried with RewriteRule using both absolute and relative path like below:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^resources/(.*)$ r/$1
What happen in real
It works half and half!
The requests from root domain (www.example.com) are loaded correctly, but from stylesheet where loaded.
E.g.
index.html requests resources/styles/home.css and works;
home.css requests resource/img/bg.png and doesn't work.
In web inspector, the request is shown http://www.example.com/resources/styles/resources/img/bg.png
and it's look like there was no RewriteRule.
Final
How can I fix this problem? And, please, explain step-by-step the answer so I can better understand my mistakes.
Your rewrite rule is working just fine. The problem is that you're using relative paths. resources/styles/home.css relative to www.example.com is www.example.com/resources/styles/home.css. However, resource/img/bg.png relative to www.example.com/resources/styles/home.css is www.example.com/resources/styles/resources/img/bg.png. This path resolution is happening in the browser, your rewrite rules have absolutely nothing to do with it.
Fix your CSS file and other relative references to point to the correct files. In your CSS file you should point to ../img/bg.png or /resource/img/bg.png.
I want to create a single page such as this:
http://www.mywebsite.com/special/index.html
But anything in the /special/ folder should be able to load the index.html page. For example, if you go to
http://www.mywebsite.com/special/another-page.html
It should still load the index page but not change the URL in the browser or to search engines. Basically, you should be able to go to any page in the /special/ folder, keep the URL the same as you enter, but always load the index.html page. Any ideas?
A 404 or 301 redirect wouldn't work because that changes the URL in the browser and to search engines...
Thanks in advance!
A 404 redirect would not help, but a custom 404 handler would:
error404.php:
<?php
include('path/to/special/index.html');
?>
Assuming .html is a static or PHP page. If it is something else, youse the equivalent construct of that environment.
Using apache mapping it should be possible. I don't how to exactly do that but this doc http://httpd.apache.org/docs/2.0/urlmapping.html probably has the answer.
It is possible to use patterns to map URL to filesystem locations.
I think (untested) something like this would work in an .htaccess file in the special directory if you have the ability to use rewrite rules:
RewriteRule ^.*$ index.html
You would have to symlink index.html in the special directory to the real index.html.
The ^.*$ just means (beginning of line)(any amount of anything)(end of line) - basically a wildcard; there might be a better way of writing it.