Style Sheets and hiding .html extension

混江龙づ霸主 提交于 2019-12-22 18:16:05

问题


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.


回答1:


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 !




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/16430500/style-sheets-and-hiding-html-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!