1

How can I achieve this

Existing URL :
newsDetails.php?cid=1&nid=$5698

so I want to rewrite to new URL :

news-1/details/1000

contents of .htaccess file :

# For security reasons, Option all cannot be overridden.
#Options All -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^news-(\d+)/[^/]+/(\d+)/?$ newsDetails.php?cid=$1&nid=$2 [L,NC,QSA]
Danish
  • 115
  • 7

2 Answers2

1

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^news-(\d+)/[^/]+/(\d+)/?$ newsDetails.php?cid=$1&nid=$2 [L,NC,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • **Not working** comment is of little use. Need answers of all of these questions. Q0. What URL are you entering in your browser? Q1. Is this the only .htaccess or do you have another? Q2. Is this the only code in this .htaccess? If you have some other code then please post it in question? Q3. What is full path of above .htaccess? – anubhava Jan 13 '15 at 05:32
  • www.mysite.com/newsDetails.php?cid=1&nid=5698 there is no any .htaccess file in this folder (mysite.com). the code in .htaccess is in question now. the full path will be www.mysite.com/.htaccess – Danish Jan 14 '15 at 05:57
  • That's the problem. This rule is to support your pretty URL i.e.: `www.mysite.com/news-1/details/1000`. – anubhava Jan 14 '15 at 10:32
  • I did not get you. I mean there is no any OTHER .htaccess file in this folder (other than only one .htaccess). Where is the problem ? – Danish Jan 14 '15 at 10:54
  • FYI. wordpress works nicely on this server (difference folder) – Danish Jan 14 '15 at 10:56
  • This rule is for supporting your pretty URL scheme. It is not for redirecting `www.mysite.com/newsDetails.php?cid=1&nid=5698 ` back to `www.mysite.com/news-1/details/5698`. Since we don't know value of `details` here. – anubhava Jan 14 '15 at 10:57
  • In question I have clearly mentioned newsDetails.php?cid=1&nid=$5698 to what url – Danish Jan 14 '15 at 12:29
  • this is not for WP. so I don't feel mentioning wordpress – Danish Jan 14 '15 at 12:29
  • In the very first comment on your question I wrote **htaccess cannot query the database** If `detail` of the new url isn't in the original URL i.e. `www.mysite.com/newsDetails.php?cid=1&nid=5698` it cannot be added in the pretty URL. – anubhava Jan 14 '15 at 14:51
0

I prefere to realize it full in PHP. U need only a rewrite like

....
RewriteRule ^(.*)$ /index.php/$1 [L]

If u want to use your existing URLs, then make PHP redirects from the old files.

A. Blub
  • 792
  • 1
  • 5
  • 10