[Resolved] Mod-rewrite

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
mod-rewrite

Well i am trying to use mod re-write and have put this in a .htaccess file but it does nothing:
Code:
RewriteRule ^category/([0-9]+)?$ index.php?menu=showcat&cid=$1

Any ideas?

I'm trying to change "index.php?menu=showcat&cid=4"
to like
/category/4/

please help

thanks!
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
A guess...

RewriteRule ^/category/(.*) /index.php?menu=showcat&cid=$1

If that works please reply back :)
 
Last edited:
0
Close
Is there something i need to actually put into my php script itself to pick up the use of mod-rewrite??
 
0
Close
No... but your .htaccess should look like this

Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/category/(.*) /index.php?menu=showcat&cid=$1

It'll only work if you have mod_rewrite.c, and of course my regex might be wrong.
 
Last edited:
0
Close
Well i put that in the .htaccess and it did nothing, think it's time to contact my host, they said it's on.
 
0
Close
Code:
RewriteEngine on
RewriteRule ^category/([0-9]+)$ http://www.YOUR_DOMAIN.com/index.php?menu=showcat&cid=$1 [L]

There is no need for the conditional statements that Danltn used. Also this only works if there is no trialling slash. If you want it to work if there is a trialling slash have:-

Code:
RewriteEngine on
RewriteRule ^category/([0-9]+)/?$ http://www.YOUR_DOMAIN.com/index.php?menu=showcat&cid=$1 [L]

This definitely works as I have just tested it on my own server.
 
0
Close
Have you actually changed the links to use the new, rewritten URLs?

Also, here is a .htaccess that will work for URLs with and without a slash, provided the categories are alphanumeric.

Code:
RewriteEngine On
RewriteBase /
RewriteRule ^category/([a-zA-Z0-9]+)(/|)?$ index.php?menu=showcat&cid=$1
 
0
Close
The following should work
(tested myself)
Code:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+) /index.php?menu=showcat&cid=$1
Joe
 
Last edited:
0
Close
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^category/([0-9]+)(/|)?$ index.php?menu=showcat&cid=$1

Final go. :p
 
0
Close
Danltn said:
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^category/([0-9]+)(/|)?$ index.php?menu=showcat&cid=$1

Final go. :p

Why are you checking for a | in your regexp? (looking back it seems because mikor is using it, are you expecting it to have some special behavior?)

TheArbiter said:
The following should work
(tested myself)
Code:
RewriteEngine On
RewriteRule ^category/(.*) /index.php?menu=showcat&cid=$1
Joe

If the category is going to be numerical it would be best to ensure that it is in fact numerical (although the check should also be made within the script)
 
Last edited:
0
Close
peter@flexiwebhost said:
Danltn said:
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^category/([0-9]+)(/|)?$ index.php?menu=showcat&cid=$1

Final go. :p

Why are you checking for a | in your regexp?

I just edited Mikor's, so it would only work it 0-9
 
0
Close
Danltn said:
I just edited Mikor's, so it would only work it 0-9

Yeah noticed that after I posted.

Anyway as I posted above, if he wants to allow slashes you simply do:-


Code:
RewriteEngine on
RewriteRule ^category/([0-9]+)/?$ http://www.YOUR_DOMAIN.com/index.php?menu=showcat&cid=$1 [L]

The question mark after the slash makes it optional. I prefer also to put the name of the domain in the path of the rewritten rule as if you do not it can cause problems. In this case if you do not use the domain name then you will be forwarded too:-

http://www.YOUR_DOMAIN.com/category/index.php?menu=showcat&cid=1 if you tried http://www.YOUR_DOMAIN.com/category/1 I came across this exact problem while developing my current project.
 
0
Close
"The question mark after the slash makes it optional."

I remember this :hehe:

"I prefer also to put the name of the domain in the path of the rewritten rule as if you do not it can cause problems. In this case if you do not use the domain name then you will be forwarded too:-"

I agree with this as well, although it can cause some fussy problems if you plan on changing domains, but that is rare.

Brilliant job as always Peter :)
 
0
Close
peter@flexiwebhost said:
Why are you checking for a | in your regexp? (looking back it seems because mikor is using it, are you expecting it to have some special behavior?)
The pipe symbol (|) is an OR. Whenever allowing for both a / or not a /, I use (/|)
 
0
Close
Mikor said:
The pipe symbol (|) is an OR. Whenever allowing for both a / or not a /, I use (/|)

the proper syntax for that behavior is the ? which is specifically for something that is optional
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back