Web Hosting Talk







View Full Version : URL Rewriting not working


ashras99
03-11-2010, 01:41 PM
On my apache server. My dynamic page url is like this..

www.xyz.com/job.php?id=2245

now i tried the following rule but not working, please tell me what's wrong.


Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^detailjob/([0-9]+)(.*)\.html$ job.php?id=$1 [NC,L]



** mod rewrite is enabled and other rules are working fine.

Driver01
03-12-2010, 05:26 AM
try it without the directory seperator i.e.

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^detailjob-([0-9]+)\.html$ job.php?id=$1 [NC,L]

I also removed the second pattern because you don't need it.

This will give you a link of:

http://www.yourdomain.com/detailjob-2345.html

ashras99
03-12-2010, 05:34 AM
i tried this but also not working.

RewriteRule ^detailjob-([0-9]+)\.html$ job.php?id=$1 [NC,L]

Driver01
03-12-2010, 09:08 AM
Well that should work lets try

RewriteEngine On
RewriteBase /
RewriteRule ^detailjob-([0-9]+)\.html$ job.php?id=$1 [NC]

or try something simple to make sure mod-rewrite is working:

RewriteRule ^oldstuff\.html$ newstuff.html

create the appropriate files on the server to test.

ashras99
03-12-2010, 09:27 AM
second rule for test is working....

but actual rewriterule is not working and when RewriteEngine On is ON for other rules then not required to mention again.

Driver01
03-12-2010, 09:53 AM
second rule for test is working....

but actual rewriterule is not working and when RewriteEngine On is ON for other rules then not required to mention again.

Yes but this is the base directory right? you have other rules in different directories? or domains?

I am presuming you have the domain xyz.com and in the base directory is a file job.php which uses a query string ?id=1234 which creates a url query string of http://www.xyz.com/job.php?id=1234, with a .htaccess file in the same directory using:

RewriteEngine On
RewriteBase /
RewriteRule ^detailjob-([0-9]+)\.html$ job.php?id=$1 [NC]

so when using a link http://www.xyz.com/detailjob-1234.html should resolve to the same as http://www.xyz.com/job.php?id=1234 without re-write.

ashras99
03-12-2010, 10:16 AM
http://www.xyz.com/detailjob-1234.html[/url] should resolve to the same as http://www.xyz.com/job.php?id=1234 without re-write.

You are right but not tried the above way... and no intention of trying... because following mod_rewrite rule is not working.

RewriteEngine On
RewriteBase /
RewriteRule ^detailjob-([0-9]+)\.html$ job.php?id=$1 [NC]

ashras99
03-12-2010, 10:17 AM
And my other rules are added in the base directory .htaccess and working fine.

Driver01
03-12-2010, 10:21 AM
why not just paste the .htaccess file here so I can see what is working?

ashras99
03-12-2010, 10:27 AM
well there is nothing wrong in my htaccess file, so i dont need to post here.

Driver01
03-12-2010, 10:44 AM
well there is nothing wrong in my htaccess file, so i dont need to post here.

Ok well I have tested the exact same rewrite and it works for me, I only suggested that so I could compare a rule that is working with the one that isn't?

btw when you say not working you mean you get the url doesn't exist error?

all you are doing is rewriting "detailjob-($1).html" to "job.php?id=($1)" matching the id number and adding it. So by all intense purposes if the url "job.php?id=1234" is a valid then the re-write should work?

ashras99
03-12-2010, 11:06 AM
when i say not working... that means when i access the url then that not converted into the http://www.xyz.com/detailjob-1234.html

that always stays on http://www.xyz.com/job.php?id=1234

never get any url error.

Driver01
03-12-2010, 11:48 AM
ah...!
Just to verify again, you are coding this uri into your documents:

<a href="http://www.xyz.com/detailjob-1234.html">MyLink</a>

this when clicked gets rewritten by apache mod re-write according to the htaccess rules into:

http://www.xyz.com/job.php?id=1234

and sent to that url not the other way around.

The idea of re-write is for you to put nice uri into your html document replacing the long query strings and set apache mod re-write some rules and expressions to match if it matchs those expressions then it will send them to the destination which is provided i.e.

expression to match:
^detailjob-([0-9]+)\.html$

destination url
job.php?id=1234

ashras99
03-12-2010, 11:53 AM
i m accessing this in browser... http://www.xyz.com/job.php?id=1234 and this not changing to http://www.xyz.com/detailjob-1234.html

Driver01
03-12-2010, 12:06 PM
No it won't? you access this in the browser http://www.xyz.com/detailjob-1234.html and it will take you to http://www.xyz.com/job.php?id=1234.

read the previous post, your getting confused in how re-writing works.

ashras99
03-12-2010, 12:19 PM
my dynamic links take me to http://www.xyz.com/job.php?id=1234.

so how they take me to http://www.xyz.com/detailjob-1234.html, how the url in browser and for search engine change?

Driver01
03-12-2010, 12:35 PM
with your mod re-write set as I suggested visit this link in the browser:

http://www.xyz.com/detailjob-1234.html

and it will take you to the same destination as your dynamic link without anyone knowing any different, thats mod re-writing.

the nice link is just your dynamic link in disguise...its an alias

ashras99
03-12-2010, 12:39 PM
but if user and search keep seeing.. http://www.xyz.com/job.php?id=1234

then what's the use of that... then some rule also required to change the url display in browser.

Driver01
03-12-2010, 12:50 PM
No thats the point, they don't see the dynamic url at all, user or search engine, they see http://www.xyz.com/detailjob-1234.html.

where-ever you use http://www.xyz.com/jobs.php?id=1234 in your document change it to the new link.

BTW does it work now?

ashras99
03-12-2010, 01:30 PM
if i access this url http://www.xyz.com/detailjob-1234.html then loading the dynamic url... but only text... no CSS and images are loading.

secondly, i have to make changes in the script because users currently seeing a dynamic link. http://www.xyz.com/job.php?id=1234

Driver01
03-12-2010, 01:41 PM
the css and images are just a path problem i.e. your not using the correct path, try full paths. Yes unfortunately you have to change the links in the script.
I am glad its working for you, my pleasure.