5

I am trying to rewrite the URL through the htaccess file so that the following URL

www.domain.com/subfolder/index.php?key

can be accessed by:

www.domain.com/subfolder/index.php/key

the specified "key" will determine which page to include in the PHP code. I have the following htaccess code already, however the CSS, JS, images and such are not being displayed when using the second (clean) URL. Any ideas as to what could be the issue?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
RewriteRule ^index.php/([a-zA-Z0-9/_]+)$ index.php?key=$1
Sumurai8
  • 20,333
  • 11
  • 66
  • 100
PHPNorton
  • 159
  • 6
  • You must use *absolute* paths instead of *relative* paths for all your html resources (css, js, images, href links, etc) – Justin Iurman Jan 02 '15 at 15:43
  • Thanks for the answer! The location of the webapp may change though, that would be a pain to change all of the paths, I am looking for an htaccess solution. – PHPNorton Jan 02 '15 at 15:45
  • 1
    You can't handle it with htaccess. All you can do is using **absolute** paths. You can do it by adding a tag right after each `` in your pages: ``. See my answer on a similar question here: http://stackoverflow.com/questions/25630373/a-way-to-avoid-apache-alias-path-to-include-html-resources-like-css/25632455#25632455 – Justin Iurman Jan 02 '15 at 15:48
  • Thanks Justin. Added it to the head and it worked as expected! – PHPNorton Jan 02 '15 at 15:55

4 Answers4

6

When you use relative url's, the browser will dynamically create a complete url by using the url of the resource it loaded. In other words: It uses the url as it is displayed in the address bar. In your case (www.domain.com/subfolder/index.php/key) it tries to load any relative url relative to www.domain.com/subfolder/index.php/. Your resources are however not located there.

You have two options to resolve this problem:

  • Convert your relative url's into absolute url's, at least absolute to the domain root. Something like <img src="img/unicorn.png"> should be turned into <img src="/path/to/img/unicorn.png">.

  • Add a base to your head element. This base will be used instead of the url of the resource to calculate the complete url. You should add <base href="/"> to your <head> element. / will then be used as the base of any relative url.

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
1

Did you try the base URL? That is the best solution I have come across. Just put one in the head and then you can even give it an ID and access the URL with javascript.

<base id='baseElement' href="your path goes here">  <!--possibly generated with serverside code-->

Access with jquery

var base = $('#baseElement').attr('href');
  • Any additional information (code examples, documentation) on how to implement this idea would improve this answer. – ryanyuyu Mar 10 '15 at 15:06
1

Actually there is a solution with .htaccess !

Assuming that your index.php is located in /mysite/ and your ressources files are in /mysite/scripts/, /mysite/images/ and/mysite/style/ like mines, you could go like this :

RewriteEngine on
#first exclude those folders
RewriteBase "/mysite/"
RewriteRule ^(style|scripts|images)($|/) - [L]
#then go with your rules (the [L] terminates the ruling)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
RewriteRule ^index.php/([a-zA-Z0-9/_]+)$ index.php?key=$1

You can see .htaccess mod_rewrite - how to exclude directory from rewrite rule for few other ways

Community
  • 1
  • 1
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
-1

Also, you can try to add this to your HTaccess:

RewriteBase /
  • Why would that do anything? RewriteBase is the base that is used for rewrites, but since this problem is unrelated to the actual rewrites happening it would have no effect. – Sumurai8 Mar 28 '15 at 14:25