Jump to content

URL rewriting adds another page/


kartul

Recommended Posts

Hello everyone

 

I got little problem with URL rewriting and styles, js files and pictures. Current code I have:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule index index.php [NC] #just index, no file ext.
RewriteRule page/(.*) index.php?page=$1 [QSA,NC,L] # should display myhost.com/test/page/13

 

Now the problem is, the first rule works, entering just index without ext. works. But the second rule works fine first time (myhost.com/test/page/2) but next page, it adds another page/ to the url (myhost.com/test/page/page/3) and browser displays error that the links is expired.

when I change the last rule to:

RewriteRule page/[(0-9)+] index.php?page=$1 [QSA,NC,L]

the index.php?page=13 doesn't get there - I tried to echo it out and it was empty. I'm stuck now cause the QSA flag is there and it should add this new number, i.e 13 there.

Link to comment
https://forums.phpfreaks.com/topic/210627-url-rewriting-adds-another-page/
Share on other sites

The problem is with your HTML not your rewrites. You almost certainly have something along the lines of...

 

<script type="text/javascript" src="js/cool_mojo.js"></script>

 

Despite the fact you have rewritten the file on the server side, the client has no way of knowing this. All the browser knows is that it it requested '/page/something', so when it encounters a path of 'js/cool_mojo.js', it thinks the path is '/page/js/cool_mojo.js'. The solution to your problem is to simply use paths relative to the root '/' a.k.a absolute paths.

 

<script type="text/javascript" src="/js/cool_mojo.js"></script>

Damit! I had a problem and I solved it but got another problem. Now both problems are there. Damit, my mistake. I have the styles, js and images fixed. I added the full url there. They work now fine.

 

The real problem what I'm now facing is the pagination (content split between pages) urls. Currently I have following line:

RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [QSA,NC,L]

The pagination works, content get's splited up between pages but the url is messed up. first page get's served as myhost.com/test/page/2, next url is myhost.com/test/page/page/3 (it works tho, right content is shown), next one again, one more page there (myhost.com/test/page/page/page/3). if I go back, it still adds another page/ there. Now, my question is, how can I get rid of the page/'s there and only show one?

 

Again, I apologies for the wrong info in first post.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.