Web Hosting Talk







View Full Version : Help with .htaccess rewrite rules


imoracle
06-19-2008, 05:42 PM
Hello All,

I am trying to achieve the following things on my site. I have an answers section, under which I have sub categories like internships, jobs etc etc.

http://mysite.com/answers goes to http://mysite.com/answers.php

This works fine, but when I try to do:

http://mysite.com/answers/jobs which must go to http://mysite.com/jobs.php or http://mysite.com/answers.php?q=jobs

it doesn't go to either of them I have tried several rewrite rules.

Another thing which is important is:
I also want to provide my users a public url like:

http://mysite.com/username which goes to http://mysite.com/publicprofile.php?q=username

This is also working fine. Just that the internships and jobs link doesnt work fine. I get a page with no css or javascript.

Here are the rewrite rules I am using:

RewriteRule ^answers answers.php
RewriteRule ^answers/ answers.php

RewriteRule ^answers/([a-zA-Z0-9_-]+)$ answers.php?q=$1
RewriteRule ^answers/([a-zA-Z0-9_-]+)/$ answers.php?q=$1

RewriteRule ^([a-zA-Z0-9_-]+)$ publicprofile.php?q=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ publicprofile.php?q=$1

I tried the above set. http://mysite.com/answers and http://mysite.com/username works fine. But http://mysite.com/answers/jobs doesn't work fine. It doesn't go as http://mysite.com/answers.php?q=jobs internally.

Can someone kindly suggest me a solution to it ??

Thanks in advance,
Imoracle

martianz
06-20-2008, 06:34 AM
I tested this out and the following code worked:

RewriteEngine On
RewriteRule answers/(.*) $1.php

This will take hxxp://mysite.com/answers/jobs to hxxp://mysite.com/jobs.php.

Hope this helps ..

portal
06-21-2008, 05:34 AM
When using mod_rewrite your linking must be absolute. For example,

instead of <link rel="StyleSheet" href="css/default.css" media="screen" />

you would do

<link rel="StyleSheet" href="/css/default.css" media="screen" />

Hello All,

I am trying to achieve the following things on my site. I have an answers section, under which I have sub categories like internships, jobs etc etc.

http://mysite.com/answers goes to http://mysite.com/answers.php

This works fine, but when I try to do:

http://mysite.com/answers/jobs which must go to http://mysite.com/jobs.php or http://mysite.com/answers.php?q=jobs

it doesn't go to either of them I have tried several rewrite rules.

Another thing which is important is:
I also want to provide my users a public url like:

http://mysite.com/username which goes to http://mysite.com/publicprofile.php?q=username

This is also working fine. Just that the internships and jobs link doesnt work fine. I get a page with no css or javascript.

Here are the rewrite rules I am using:

RewriteRule ^answers answers.php
RewriteRule ^answers/ answers.php

RewriteRule ^answers/([a-zA-Z0-9_-]+)$ answers.php?q=$1
RewriteRule ^answers/([a-zA-Z0-9_-]+)/$ answers.php?q=$1

RewriteRule ^([a-zA-Z0-9_-]+)$ publicprofile.php?q=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ publicprofile.php?q=$1

I tried the above set. http://mysite.com/answers and http://mysite.com/username works fine. But http://mysite.com/answers/jobs doesn't work fine. It doesn't go as http://mysite.com/answers.php?q=jobs internally.

Can someone kindly suggest me a solution to it ??

Thanks in advance,
Imoracle

bear
06-21-2008, 08:04 AM
When using mod_rewrite your linking must be absolute. For example,

instead of <link rel="StyleSheet" href="css/default.css" media="screen" />

you would do

<link rel="StyleSheet" href="/css/default.css" media="screen" />

For the record, that is root relative, not absolute. Absolute would be the full http path, IE: "http://www.example.com/path/file.css"

Doc relative: ../path/file.css
Root relative: /path/file.css
Absolute: as above

portal
07-12-2008, 08:20 PM
Was late at night ;)

For the record, that is root relative, not absolute. Absolute would be the full http path, IE: "http://www.example.com/path/file.css"

Doc relative: ../path/file.css
Root relative: /path/file.css
Absolute: as above