1

This might look like a server question but I think it belongs more on SO that on SF.

Long story short, I have a Nginx server that automatically aggregates .html to my pages in the backend so that the users only see :

mysite.com/somepage

instead of

mysite.com/somepage.html

So in my code, my links look like this :

  <ul>
       <li class="current"><a href="somePage"></a></li>
       <li class="menu"><a href="somePage2"></a></li>
  </ul>

Then nginx works its magic and adds a .html to it secretly.

Of course, this doesn't work in my dev environment (environment controlled by CodeAnywhere.com) so for it to work I would have to add the extensions like so :

 <ul>
       <li class="current"><a href="somePage.html"></a></li>
       <li class="menu"><a href="somePage2.html"></a></li>
 </ul>

Since changing those href by hand everytime I want to go to production and adding back the .html when I pull the copy and want to code, there must be an easy way around this.
Anyway I can work around that using HTML or JS? Or any other outside the box ideas..would be greatly appreciated.

phadaphunk
  • 12,785
  • 15
  • 73
  • 107
  • 1
    Have you looked into setting up a `.htaccess` file? – Steve Sep 04 '15 at 00:10
  • 1
    Are you using a webserver on your development machine? Can you set up a URL rewrite on that server? – raduation Sep 04 '15 at 00:10
  • @Steve I can't use such a file since I'm on nginx and not apache. Altough my dev envrionment might be setted up using apache I guess I'll look into that. – phadaphunk Sep 04 '15 at 00:15
  • @raduation I cannot. I would have to contact the codeanywhere.com support as they are managing my dev machine – phadaphunk Sep 04 '15 at 00:16
  • @phadaphunk it looks like `.htaccess` can be converted to its nginx equivalent: http://stackoverflow.com/questions/8711678/change-apache-htaccess-file-to-be-used-with-nginx does that help? – Steve Sep 04 '15 at 00:17
  • @Steve It is already setup on my prod envrionment using something similar to what you linked. The issue is what can I do on my dev environment to be able to live with Hrefs that do not have the .html extensions – phadaphunk Sep 04 '15 at 00:18
  • From what it sounds like you want to do the opposite of what your question title is? Basically you want to rewrite the `foo` to `foo.html` in your ide? correct me if I'm wrong... – l'L'l Sep 04 '15 at 00:25
  • @I'L'I totally correct. I'll modify the title – phadaphunk Sep 04 '15 at 00:32

2 Answers2

2

In your codeanywhere file explorer you can likely add rewrite directives to an .htaccess file:

This info is from codeanywhere:

  1. If the .htaccess file is not shown in File explorer you have to add your server as SFTP.
  2. If you want to edit .htaccess files make sure that your server supports it and that you have right type of the server added.

In the .htaccess file try adding this rule:

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

That should rewrite foo.html to foo

More info here about .htaccess rewrite rules.

Community
  • 1
  • 1
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • 1
    Somehow the htaccess file is ignored at runtime. I'll contact codeanywhere to know exactly how they need it setup. – phadaphunk Sep 04 '15 at 00:32
  • Yeah probably a good idea; it seems there might be some specific details about it which their documentation doesn't contain. – l'L'l Sep 04 '15 at 00:35
0

Put this in a temporary script tag:

document.addEventListener("click",function(event){
  if(event.target.tagName=="A"){
    if(!event.target.href.endsWith(".html"))event.target.href+=".html";
  }
});
<a href="something">hey</a>
Isaac
  • 11,409
  • 5
  • 33
  • 45
  • This looks legit when I read the code but it doesn't work when I run it tho.. It simply doesnt append a .html at the end. – phadaphunk Sep 04 '15 at 00:30