2

I have a news (blog) site that returns urls in the following format when individual posts are selected:

website.net/sitenews.php?q=posts/view/postname/12

I am seeking to rewrite the url so that it reads:

website.net/sitenews.php/posts/view/postname/12

or any other way where the ?q= is removed for purpose of removing the ? so that the url can be accessed by facebook's like button as the facebook url linter does not parse query strings.

In the htdocs .htaccess file in the root directory I have tried the following:

Options +FollowSymLinks 

RewriteEngine on

RewriteCond %{QUERY_STRING} q=  

RewriteRule (.*) website.net/sitenews.php/$1? [R=301]

This successfully removes the q=? however the rest of the string (posts/view/postname/12) is not returned and the url now looks as follows:

 website.net/sitenews.php/sitenews.php

Does anyone have any suggestions to help me complete this url_rewrite?

2 Answers 2

2

Try this instead in your .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]

R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string

%1 is capture group for query string q= (whatever comes after q=)
$1 is your REQUEST_URI
Sign up to request clarification or add additional context in comments.

4 Comments

anubhava, the code above generates website.net/sitenews.php/posts/view/postname/12/posts/view/postname/12/posts/view/postname/12
@Hamy: Sorry I had a minor typo in above rule which was causing infinite re-directions. Please see my edited answer above. I have tested it and it should work without problems.
Thanks for your answer but how can I get rid of the .php as well in between the generated link website.net/sitenews.php/posts/view/posts ?
@Rajat: Please check my answer here: stackoverflow.com/questions/6062537/… if that doesn't meet your requirements then pls create a new question on SO and send me the question link in comments here.
0

If you are using any CMS, like wordpress, or joomla or SE, then you have option to do that else you need to have an .htaccess file where you can write the code, refer this links

http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

http://www.webmasterworld.com/forum92/2545.htm

http://www.google.com/#sclient=psy&hl=en&q=htaccess+change+the+url&aq=0p&aqi=p-p1g4&aql=&oq=htaccess+&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=c875dd2b8adea15a

Comments

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.