Jump to content

What's wrong with this 3 lines of code ?


Rommeo

Recommended Posts

There are just 3 lines of code not working with in my htaccess file

1-)

One section uses links like

 

mysite.com/34/hello+world

 

here I do not do any process with hello world part, it goes to

 

mysite.com/index.php?pageid=34

 

I was trying to use this code which is not working

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

 

2-) Same as one but this time I m using both: like

 

mysite.com/12/my+category

 

should go to

 

mysite.com/index.php?cat=12&subcategory=my+category

 

I tried to use this one which is not working again

RewriteRule ^([a-zA-Z0-9_]+)/([-A-Za-z0-9_]+)/?$   index.php?cat=$1&subcategory=$2

 

3-) This one I m using for photos which is not working also :

mysite.com/i/12-1

 

should go to

mysite.com/showphoto.php?id=12-1

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

 

Also I m wondering if the declarations are correct ? cause I m using special characters like "+" and "-". Can someone please help me to solve this problem ?

 

Thank you in advance.

 

 

 

 

How can I declare "+" sign in my codes. Cause I m using "+" and I m wondering if plus sign is not declared in my codes.

 

 

Link to comment
Share on other sites

1 and 2 are mutually exclusive, you can't possible have a site that supports both objectives. Just look at the  two example URLs...

 

mysite.com/34/hello+world

mysite.com/12/my+category

 

They use the EXACT same format, how is Apache supposed to know the first one is a page and the second is a category? Moving on to look at the actually code you provided for 1-).

 

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

 

This assumes that the URL begins with a p, which it doesn't. It's actually beginning with a number. It would also (assuming it worked) forward the whole end of the URL (34/hello+world). As for 2-), that pattern looks alot more 'correct'. You will need to add + to the character class though, since you are seperating your words with +'s.

 

RewriteRule ^([a-zA-Z0-9_+]+)/([-A-Za-z0-9_+]+)/?$   index.php?cat=$1&subcategory=$2

 

With 3-) I'd recomend putting the dash as the last character in the characterset (ie before the underscore) or at the start as in the other pattern. Since it is a Regular Expression metacharacter having it where you have it could potentially cause problems.

 

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

Link to comment
Share on other sites

Thank you for the explanation.

At first I tried the second code and I disabled the same kind of codes.

It works. . But the problem is it's without any design. I mean just text. My images, css, tables are lost. It's like a text in notepad. Why it's it so ?

 

( also tried it while others are enabled, it's same pure text. )

 

And for the third one can you give me any example please ?

should I use the images like ?

mysite.com/i/12-1-

 

Link to comment
Share on other sites

I m working on my try-out server :(

Can tell you if you want to know anything

I m wondering if the code should supply the exact needs.

like :

RewriteRule ^([0-9]+)/([A-Za-z+]+)/?$  index.php?cat=$1&subcategory=$2

 

The first part will be just numbers. So this might be correct if I m not wrong ?

Link to comment
Share on other sites

Assuming the the pattern is always similar to the example URLs ie a number followed by a forward slash followed by the characters a-z or +, that would be fine. The reason for images etc not working is probably because when the browser requests them your pattern is matching them. Matching your pattern more precisely in the manner you have done will help with this, you will also want to use Rewrite Conditions. For example something like...

 

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^([0-9]+)/([A-Za-z+]+)/?$  index.php?cat=$1&subcategory=$2

 

That basically will only forward the URL if the requested file/directory doesn't exist.

Link to comment
Share on other sites

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.