Jump to content

Recommended Posts

Hi,

 

I am new into this. However, I have been using one line Rewrite rule to rewrite those URL's that are using single variable. Here is what is in my .htaccess

 

RewriteRule ^(.*)\.html$  /index.php?pg=detail&catTitle=$1 [nc]

 

and the a href code is as follows.

 

 <a href="<?=$webPath?><?= str_replace(' ', '-', $viewcat['contentURL'])?>.html" >

 

Till here it works perfectly. Now I wish to re write for category. Here is what I am trying to do

RewriteRule ^category/(.*)/(.*).html$ /index.php?pg=dssGallery&catId=$1&catTitle=$2 [nc]

 

here is the a href code.

  <a href="<?=$webPath?>category/<?= $viewcat1[pk_id]?>/<?= str_replace(' ', '-', $viewcat1[cat_Title])?>.html" >

 

Now the problem is.. The second rule is also going to the first page. It's not reading the second rule at all.

 

Please take a look and let me know where I am wrong.

 

I'll be really greatfull.

 

Link to comment
https://forums.phpfreaks.com/topic/188942-second-variable-not-picking-the-values/
Share on other sites

Your PHP code is fine. Its your rewriterules which are at fault. I do not recommend using (.*) as it is greedy. You should be more specific with your regex partterns. Such as if you want to match only numbers use ([0-9]+), or for strings use ([A-Z0-9\-_]).

 

I'd setup your rules as such

RewriteRule ^category/([0-9]+)/([A-Z0-9\-_]).html$ /index.php?pg=dssGallery&catId=$1&catTitle=$2 [L,NC]
RewriteRule ^([A-Z0-9\-_])\.html$  /index.php?pg=detail&catTitle=$1 [L,NC]

Your PHP code is fine. Its your rewriterules which are at fault. I do not recommend using (.*) as it is greedy. You should be more specific with your regex partterns. Such as if you want to match only numbers use ([0-9]+), or for strings use ([A-Z0-9\-_]).

 

I'd setup your rules as such

RewriteRule ^category/([0-9]+)/([A-Z0-9\-_]).html$ /index.php?pg=dssGallery&catId=$1&catTitle=$2 [L,NC]
RewriteRule ^([A-Z0-9\-_])\.html$  /index.php?pg=detail&catTitle=$1 [L,NC]

Just out of curiosity wildteen88, why choose to escape the dash rather than simply place it after the underscore?

 

[A-Z0-9_-]

 

I assume this Regex convention passes through to mod_rewrite. At the end of the day it makes no difference, I'm just wondering if you have a reason for doing it that way.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.