Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following url:

http://www.mysite.com/test/index.php?topic=rules

I want to rewrite it to:

http://www.mysite.com/test/topic/rules

I tried the following:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^topic/([a-zA-Z0-9]+)/$ index.php?topic=$1

Didn't do anything. I used a 3rd party website to generate this rewrite code so maybe it's incorrect, I dunno. Is there anything in Apache that could be causing this to not work?

share|improve this question
    
Where's that rule configured? –  Shane Madden May 9 '12 at 3:41
    
Where's that final slash in the URL? –  Ignacio Vazquez-Abrams May 9 '12 at 3:45
    
It's in .htaccess in the test directory along with the index.php. I don't know anything about missing a slash. That's why I'm asking the question in case something is wrong with the rule... –  Jakobud May 9 '12 at 5:12
    
@Jakobud Your rule specifies a trailing slash. So, http://www.mysite.com/test/topic/rules will never be rewritten, only http://www.mysite.com/test/topic/rules/ will. Which behavior is needed? –  Shane Madden May 9 '12 at 22:08
add comment

2 Answers

You're missing the /test/ in the URL. The leading ^ your rule means "start of path". You probably want:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/test/topic/([a-zA-Z0-9]+)/$ /test/index.php?topic=$1

Or something similar.

share|improve this answer
    
Oh, I suppose it matters where the rule lives. Possibly nevermind... –  Bill Weiss May 9 '12 at 4:59
    
What if I wanted this to be path independent? What if I wanted it to work in /test/ or /test/whatever/ or /123/345/testing123/? –  Jakobud May 9 '12 at 5:14
    
You could try something like ^/(.*)/topic/([a-zA-Z0-9]+)/$ /$1/index.php?topic=$2 (totally untested, sorry) –  Bill Weiss May 9 '12 at 22:13
add comment

You cannot match the query string in RewriteRule, so you need to use RewriteCond.

RewriteCond %{QUERY_STRING} topic=(.*)
RewriteRule test/index.php test/topic/%1? [L]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.