2

I'm trying to change this:

/news/index.php?id=101&title=this-is-a-very-long-title

to this:

/news/this-is-a-very-long-title/

with htaccess rewrite rule. Any idea how?

I have tried:

Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteRule ^.+/news/index.php?title=$1 [NC,L,R] 
2
  • What have you tried? Commented Feb 10, 2013 at 4:54
  • @Nightfirecat:Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^.+/news/index.php?title=$1 [NC,L,R] Commented Feb 10, 2013 at 4:59

1 Answer 1

1

I think this is what you need:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !index\.php   [NC]
RewriteRule ^.*/([^/]+)/?   news/index.php?id=101&title=$1  [L,NC]

Maps silently

http://example.com/news/this-is-a-very-long-title/ with or without trailing slash

To:

http://example.com/news/index.php?id=101&title=this-is-a-very-long-title

In your rule the string id=101& is not present in the substitution URL, so remove it from the rewrite rule if it is not needed.

For permanent and visible redirection, replace [L,NC] with [R=301,L,NC].

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but what I need is actually the reverse of that. What i want is to map example.com/news/… to example.com/news/this-is-a-very-long-title. I will try your solution to do the reverse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.