Forum Moderators: phranque

Message Too Old, No Replies

Stick with RewriteRule

Facing problems with RewriteRule

         

Jeyush

7:21 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Hi,

I am new to mod_rewrite. So I found it very confusing to deal with it.

I am dealing with a query string. and I want my url look like this

[localhost...]

and main url is,

[localhost...]

and I write something like this to be done,

Options +MultiViews
Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule ^leasing/company/(.*)/(.*)-(.*)\.htm$ leasing-company.php?postCodeArea=$1&Search_btn=$2 [NC,L]

This rule is not working for me.

Please help me, if I miss something.

Thanks in advance.

Jeyush

7:36 am on Jun 2, 2009 (gmt 0)

10+ Year Member



This is the .htaccess file I have;

Options +ExecCGI
AddHandler cgi-script .
ErrorDocument 404 /error.php

RewriteEngine On
RewriteBase /

RewriteRule ^index.htm$ abc/index.php
RewriteRule ^(.*)\.htm$ abc/$1.php [nc]
RewriteRule ^.htaccess$ - [F]

Options +MultiViews
Options +FollowSymLinks

RewriteEngine On
RewriteBase /

I have tried this one too;

RewriteRule ^/(.*)/leasing/company/postCodeArea(.*)/btn(.*)\.htm$ /abc/leasing-company.php?postCodeArea=$1&Search_btn=$2 [L]

g1smd

8:02 am on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This was covered several times in the last week, please check recent prior threads for details.

The main point is that you need to use the new format URLs in the links on your pages. The rewrite can only happen if someone requests the new format URLs from your server.

Don't use multiple (.*) patterns in your rule [google.com...] . It is very inefficient, as it requires multiple (maybe hundreds) backup and retry operations before a match is found. Use a pattern like

([^/]+)
instead.

Make sure that the value for $3 is also passed to your script and is verified as being valid. Return a 404 status if it is not.

I would not use "index.<anything>" in your URLs. I would place a redirect ahead of all the rewrites that strips the filename off (and forces www at the same time). I would internally link to URLs that end with slash. I would use a rewrite from ^$ to abc/index.php too.

.

Be aware that the (.*) in this rule matches "everything" so your other rule can never run if it is placed after this one:

RewriteRule ^(.*)\.htm$ abc/$1.php [nc]

If you have a more specific rule that needs to run (like the one that "does not work") then that rule needs to be listed before this more general "everything" rule. And, again, your (.*) pattern can likely be replaced with something more efficient in all of your rules.

Jeyush

8:22 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Thanks for your assistance.

I write the rule once again;

like

rewriterule ^/(.*)/leasing/company/([^/]+)/([^/]+)/?$\.htm /leasing-company.php?postCodeArea=$1&Search_btn=$2 [L]

but still I cannot get proper result.

I have one page named as left.php, in this page I have combobox and one search button.

when enyone select something in combobox and press button search then by 'GET' method it redirect to leasing-company.php by the action tag of the form method.

code of Left.php

<form id="search" name="search" method="get" action="leasing-company.htm" >
<select name="postCodeArea" style="font-size:12px; width:175px;">
<option value="">-- select postcode --</option>
<?php
$postCode = "SELECT * FROM ukchl_postcode ORDER BY full_name";
$postCodeResult = mysql_query($postCode) or die(mysql_error());
while($postCodeArr = mysql_fetch_array($postCodeResult)){ ?>
<option value="<? echo $postCodeArr['short_name'] ; ?>" <? if ($postCodeArr['short_name']==$_REQUEST['postCodeArea']){echo "selected"; } ?> ><? echo $postCodeArr['short_name']."-".$postCodeArr['full_name']; ?> </option>
<?}
?>
</select>
<br /><br />
<input type="submit" name="Search_btn" id="Search_btn" value="Submit" />
</form>

It generates dynamically then how can I manage that url?

Help needed.

g1smd

8:23 am on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be aware that the (.*) in this rule matches "everything" so your other rule can never run if it is placed after this one:
RewriteRule ^(.*)\.htm$ abc/$1.php [nc] 

If you have a more specific rule that needs to run (like the one that "does not work") then that rule needs to be listed before this more general "everything" rule.

And, again, your (.*) pattern can likely be replaced with something more efficient in all of your rules. You missed some.

Jeyush

8:43 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Thanks once again,

Can you make me clear with dynamic url?

If I get one page with dynamic url, then how can I get my "RewriteRule"'s url.

RewriteRule old.htm new.htm

This works fine with static links, for them I can manually write that "old.htm" in page.

But

How I manage this in dynamic generated pages?

Please help me.

Jeyush

9:17 am on Jun 2, 2009 (gmt 0)

10+ Year Member



I again write the rule,

RewriteRule ^leasing/company/([^/]+)/([^/]+)\.htm$ /leasing-company.php?postCodeArea=$1&Search_btn=$2 [L]

But still I can't get proper result.

Please someone Please help me.

Jeyush

9:18 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Should I have to manually write the desired url in file?

Jeyush

9:20 am on Jun 2, 2009 (gmt 0)

10+ Year Member



Can I see those url rewriting in Localhost?

Jeyush

9:30 am on Jun 2, 2009 (gmt 0)

10+ Year Member




System: The following 5 messages were spliced on to this thread from: http://www.webmasterworld.com/apache/3924605.htm [webmasterworld.com] by jdmorgan - 12:50 pm on June 2, 2009 (CT -5)


I have one dynamic page which search the data as

[localhost...]

This will generate dynamically.

For this I have to write the .htaccess file and I wrote like this,

Options +ExecCGI
AddHandler cgi-script .
ErrorDocument 404 /error.php

RewriteEngine On
RewriteBase /

RewriteRule ^index.htm$ abc/index.php
RewriteRule ^(.*)\.htm$ abc/$1.php [nc]
RewriteRule ^.htaccess$ - [F]

Options +MultiViews
Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /leasing-company\.php\?postCodeArea=([^&]+)&Search_btn=([^&]+)\ HTTP/
RewriteRule ^leasing-company\.htm$ http://localhost/abc/leasing-company/%1/%2? [R=301,L]

Please kindly tell me where I go wrong in it?

I will be thankful to you.

Thanks in advance.

tangor

10:36 am on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you running this as localhost or live on the web?

Jeyush

11:01 am on Jun 2, 2009 (gmt 0)

10+ Year Member



on a localhost for now

g1smd

11:49 am on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I answered this question yesterday. Not sure where my posts went?

Jeyush

11:54 am on Jun 2, 2009 (gmt 0)

10+ Year Member



ok dear no problem. But do post because I am stick with it from 1 day.

jdMorgan

6:05 pm on Jun 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jeyush,

Welcome to WebmasterWorld!

I need to make you aware of a couple of things, one administrative and the other technical.

First, we have a relatively small number of contributors here, scattered all across the world. Please do not expect "instant" responses; Some are asleep when you get to work, while others are heading home for dinner.

Second, mod_rewrite cannot change the links on your pages. It has no effect whatsoever on your page content. Mod_rewrite works after a link on your page is clicked and the client (browser) sends a request for the linked URL to your server. After this request arrives at your server, mod_rewrite examines it and can either generate a client redirect (asking the browser to request a different URL), or it can modify the server filepath that will be used to serve content for the original client-requested URL -- for example, forwarding requests for HTML pages to a script instead of simply serving a static HTML page.

As a result, if you wish to modify the linked URLs on your pages, you will have to edit your static HTML pages, or modify the script(s) that produces your HTML pages dynamically.

Your RewriteRules look quite correct, but I think you are expecting them to do something that they cannot do.

See the threads in our Apache Forum Library for more information.

Jim