my url keys are coming from database , say if my url is abc.com ,i have used a rewrite rule

RewriteRule (.)/(.)/mcqs/(.)/(.)/(.*).html mcq.php?mcq_id=$4&page=$5

in this rewrite rule , first four (.) are coming as url keys from db, but problem is that in address bar am able to add any random text and still page is generated, and some of the keys are mixed, like say for first (.) i have given key a1 which is to be used in combination with a2,a3 and a4 (url keys of second,third and fourth(.*)) , but now a1 is used in combination with other url keys say b2 and c3 also

Member Avatar for LastMitch

@rohanbajaj222

in this rewrite rule , first four (.) are coming as url keys from db, but problem is that in address bar am able to add any random text and still page is generated, and some of the keys are mixed, like say for first (.) i have given key a1 which is to be used in combination with a2,a3 and a4 (url keys of second,third and fourth(.*)) , but now a1 is used in combination with other url keys say b2 and c3 also

RewriteRule (.)/(.)/mcqs/(.)/(.)/(.*).html mcq.php?mcq_id=$4&page=$5

If you want to create or try to create a URL rewrite then read and follow the instructions:

http://www.cyberdesignz.com/blog/website-design/url-rewriting-top-5-ways-of-php-url-rewriting/

or

Try to used this Mod Rewrite Generator:

http://www.generateit.net/mod-rewrite/

As my url keys are coming from db, so none of the above 2 links are helpful

Member Avatar for LastMitch

@rohanbajaj222

As my url keys are coming from db, so none of the above 2 links are helpful

What are the table(s)?

id-primary key subcat_id - from home page mcq_head- mcq heading enabled -0/1 keywords-appearing on page for seo keyword_tag - url key meta_tag- meta keywords content_tag- meta content description page_title -page title

Member Avatar for LastMitch

@rohanbajaj222

RewriteRule (a1)/(a2)/mcqs/(a3)/(a4)/(.*).html mcq.php?mcq_id=$4&page=$5 

I have never seem a RewriteRule like that before?

Maybe something like this I have seen:

RewriteRule ^/mcqs/([^/]*)/([^/]*)/([^/]*)/([^/]*).html mcq.php?mcq_id=$1&subcat_id=$2 [L]  

I am very confused what you are trying to accomplish here.

You're id's is very confusing!

Something like this (make more sense):

RewriteRule ^mcq_id-([0-9]+)/subcat_id-([0-9]+)$  mcq.php?mcq_id=$1&subcat_id=$1 [L]

What I did I passed 2 variable into mcq.php. You can add as many as you want.

The reason why when you click on a link or item. It will go to that url (mcq.php?mcq_id=$1&subcat_id=$1) link.

actually structure of site is 
home page---> categories---->subcategories---->Mcqs headings--->sub heading 1
                                          |                 |
                                          |                 |
                                          |                 sub heading 2
                                          Non mcq
                for every category , subcategory, mcq heading and sub heading a url key is given                          
Member Avatar for LastMitch

@rohanbajaj222

actually structure of site

Is this a Ecommerce website?

The structure you have is not going work well:

RewriteRule (.)/(.)/mcqs/(.)/(.)/(.*).html mcq.php?mcq_id=$4&page=$5

I have seen 1 url key and try it before but I have never seen 4 url keys at once?

4 url keys at once in a Mod Rewrite? I'm not sure it's gonna work.

It would be much easier just write query for those ids and fetch them instead of using URL rewrite.

4 url keys are working, the only problem am facing is that any static text can be inserted in url and still page is displayed, and urls are taking other keys also say ,a3 is to be used a4, but now a3 is used b4 and c4 also (error), and a1 and a2 are used in combination with b3 and c4 (error)also

Member Avatar for LastMitch

a3 is to be used a4, but now a3 is used b4 and c4 also (error), and a1 and a2 are used in combination with b3 and c4 (error)also

Even though you got it working but its not working correctly.

You wrote a Mod Rewrite for this:

RewriteRule (.)/(.)/mcqs/(.)/(.)/(.*).html mcq.php?mcq_id=$4&page=$5

Maybe this works only once:

You insert a1 to a2 so it becomes a2.

But it's not gonna work for the rest as you notice yourself the errors you are having.

If you think that you can used 4 url keys at once and without any errors then keep trying.

From my point of view, it's not going to work well.

The purpose of Mod Rewrite is to make sure people can access your website by a particular page or page(s) which you can used any url keys but not 4 at once.

I really don't know what else to tell you.

i own this site, coding done by programmer, can u help me in solving this problem, will provide u with details further

Member Avatar for LastMitch

can u help me in solving this problem, will provide u with details further

What URL do you need?

want to shorten the length of urls, reduce number of url heys from htaccess, and validate urls means if some random text is inserted in address bar , it should display msg as invalid url

Member Avatar for LastMitch

@rohanbajaj222

want to shorten the length of urls, reduce number of url heys from htaccess, and validate urls means if some random text is inserted in address bar , it should display msg as invalid url

Is this a Ecommerce website?

I mean you are doing alot of different urls.

I think I already mention to you that the purpose Mod Rewrite is to make sure people can access your website by a particular page or page(s).

Try to used this Mod Rewrite Generator:

http://www.generateit.net/mod-rewrite/

I copy and paste the example from the page link I provide:

For example, you may enter the URL:

youdomain/cgi-bin/shop.php?cmd=product&category=vehicles&product=bus

and transform it into

youdomain/shop/vehicles/bus.html

Do you understand how it works now?

no ,its not an ecommerce site,its an educational site
problem is that i have url keys coming from db , so cant shorten it with the help of link u provided
my gmail id is same as my userid here

Member Avatar for LastMitch

@rohanbajaj222

problem is that i have url keys coming from db , so cant shorten it with the help of link u provided

OK this is your current Mod Rewrite:

RewriteRule (.)/(.)/mcqs/(.)/(.)/(.*).html mcq.php?mcq_id=$4&page=$5

Add this to your htaccess file and try this:

RewriteEngine on
RewriteRule ^/mcqs/([^/]+)/([^/]+)\.html /mcq.php?mcq_id=$1&page=$2 [NC,R]

or try this:

RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/mcqs/([^/]+)/([^/]+)\.html /mcq.php?mcq_id=$1&page=$2 [NC,R]

If you want to see what I did read this:

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritemap

You have to play around with it. I don't have any url keys nor a database.

Do you have query to fetch the url id?

I tried with
RewriteRule ^([^/]+)/([^/]+)/mcqs/([^/]+)/([^/]+).html /mcq.php?mcq_id=$1&page=$2 [NC,R]
as am having 2 url keys before mcqs, but didnt worked
404 error was displayed

Member Avatar for LastMitch

as am having 2 url keys before mcqs, but didnt worked

If this is new website there shouldn't be any error at all.

Is your website active?

If it's active you need to create 404 error page so it can redirected to the pages.

Without it, it won't redirected.

You need to add these to your htaccess file:

DirectoryIndex index.php
ErrorDocument 404  /error.php 

Redirect /olddirectory yourdomain/newdirectory
Member Avatar for LastMitch

and after writing rewrite rule 404 page was displayed

How many sections?

Is this your table structure?

id
subcat_id
mcq_head
enabled
keywords
keyword_tag
meta_tag
content_tag
page_title

What is the url here:

You said 4 url?

This is only 2:

mcq_id=0&page=0

How does it look:

from 4 to 3 to 2 to 1

yes , this is the table structure for mcqs.
In this url
http://www.avatto.com/gate/aptitude/mcqs/reasoning/complete-the-series/104/1.html
I said there are 4 url keys one each for
1. home page category -eg gate
2. home page subcategory-eg aptitude
3. mcq head category-eg reasoning
4. mcq subheading -eg complete-the-series

later on home page categories and subcategories have to be changed , but couldnt change the url keys, as those were indexed by google

Member Avatar for LastMitch

@rohanbajaj222

later on home page categories and subcategories have to be changed , but couldnt change the url keys, as those were indexed by google

There's nothing wrong with your URL's.

I think it's your query that fetch the url key is incorrect so that's why it's not displaying the url keys.

hum......., am using this query

$QUERY="select tbl_mcq_title.id, tbl_mcq_title.mcq_title, tbl_category_master.category_name, tbl_subcategory_master.subcat_name, tbl_mcq_master.mcq_head, tbl_category_master.keywords as cat_key, tbl_subcategory_master.keywords as subcat_key, tbl_mcq_master.keywords as head_key, tbl_mcq_title.keywords as title_key from tbl_mcq_title left join tbl_mcq_master on tbl_mcq_title.mcq_m_id = tbl_mcq_master.id join tbl_subcategory_master on tbl_subcategory_master.id = tbl_mcq_master.subcat_id join tbl_category_master on tbl_category_master.id = tbl_subcategory_master.cat_id WHERE tbl_subcategory_master.id=$subcat_id order by tbl_mcq_master.mcq_head, tbl_mcq_title.mcq_title"

for displaying
http://www.avatto.com/gate/computer-science/mcqslist/10.html

Member Avatar for LastMitch

@rohanbajaj222

You're query is a JOIN.

Try this I'm not sure it's gonna work because in order to test out URL you have to test it:

//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^avatto.com[nc]
RewriteRule ^(.*)$ http://www.avatto.com/$1 [r=301,nc]

//301 Redirect Entire Directory
RedirectMatch 301 www.avatto.com(.*) www.avatto.com/$1

//Change default directory page
DirectoryIndex www.avatto.com

//Rewrite 1
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^([^/]+)/([^/]+)/mcqs/([^/]+)/([^/]+).html$ $mcq.php?mcq_id=$1&page=$2 [NC,R]

//Rewrite 2
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^(.*)/(.*)/mcqs/(.*)/(.*).html$ $mcq.php [R=301,L]

it didnt worked, i got internal server error

Member Avatar for LastMitch

What error?

error message was internal server error

I think i sent wrong query, tht was from another site

Member Avatar for LastMitch

I think i sent wrong query, tht was from another site

I create 2 Rewrite:

//Rewrite 1
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^([^/]+)/([^/]+)/mcqs/([^/]+)/([^/]+).html$ $mcq.php?mcq_id=$1&page=$2 [NC,R]
//Rewrite 2
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^(.*)/(.*)/mcqs/(.*)/(.*).html$ $mcq.php [R=301,L]

I think the issue was I put 2 rewrite that is the same that made the internal error.

This code should be in the htaccess file try this:

//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^avatto.com[nc]
RewriteRule ^(.*)$ http://www.avatto.com/$1 [r=301,nc]
//301 Redirect Entire Directory
RedirectMatch 301 www.avatto.com(.*) www.avatto.com/$1
//Change default directory page
DirectoryIndex www.avatto.com
//Rewrite 1
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^([^/]+)/([^/]+)/mcqs/([^/]+)/([^/]+).html$ $mcq.php?mcq_id=$1&page=$2 [NC,R]

then try this if the first one didn't work:

//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^avatto.com[nc]
RewriteRule ^(.*)$ http://www.avatto.com/$1 [r=301,nc]
//301 Redirect Entire Directory
RedirectMatch 301 www.avatto.com(.*) www.avatto.com/$1
//Change default directory page
DirectoryIndex www.avatto.com
//Rewrite 2
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^(.*)/(.*)/mcqs/(.*)/(.*).html$ $mcq.php [R=301,L]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.